List Files and Folders with Linux ls Command

The Linux ls command is used to list files and folders in a directory. There are tons of options to use on the ls command. I will not cover every single option that you can use with the linux ls command. I will cover the most common options for the ls command. If you don’t find what you are looking for in this Linux tutorial then I would recommend running man ls and reading the man pages for the ls command.

The usage for the Linux ls command is: ls [options] [file|directory]

You can use the ls command without any options or as many options as you like.

If you just run ls with no options it will output the list of files in the current working directory in alphabetical order. So if you are in $HOME (which is the system variable for your home directory ( /home/username ). This will also not output any .* files or directories ( files or directories that start with . as they are hidden files|directories ).

Now if you want to list all files and folders in a directory you can use ls -a or --all option. This option will output all files and folders including the files and folders that match .* ( your hidden files and folders ). You can also use ls -A or ls --almost-all which will output all files|directories except for ./ and ../ ( which ./ is current working directory, ../ is your parent directory ).

Another common option for ls is using ls -b or ls --escape which will escape any special characters like spaces. This is more useful if you can not figure out which characters need escaped or if you are writing a script that needs the characters escaped.

ls -l will output the files and directories in long list format. Which means ls -l will output your file and directory permissions, hardlinks or entries, user, group, size (in bytes), month, date, time, filename|directory name.

ls -h will display the output sizes in human readable form, the highest available measurement. ( 1K 1M 1G.. etc ). This option has to be used with an option that outputs size as well. So either ls -s or ls -l but you would combine the options, ls -lh or ls -sh will work.

You can combine a number of options for ls to get the results you want, ls -lha will display the output of all files and directories in long list format with the sizes being human readable aka the highest measurement available.

You can also pass the output onto other commands using a pipe | so that you can alter the results more to specific needs. For example if you want to just see the sizes of files, and not filename or directory names: ls -sh | awk '{print $1}' will do this for you. If you wanted to display just the permissions of the files and directories, you could use ls -l | awk '{print $1}' which will give you those results. If you want more information on scripting like this I will be covering this in another section.

I hope this beginner Linux tutorial on the Linux ls command has helped you accomplish what you need and also give you a better understanding on how the ls command works. Continue reading through the beginner Linux tutorial website for more Linux tutorials!

Comment Linux tutorial or Leave a Trackback

Post a Comment

Your email is never published nor shared. Required fields are marked *