ls [man page] - list directory contents
The very first time you login into Unix system a shell prompt will be shown on your terminal. The prompt you will see will be similar to the one below:
This prompt gives you a following information:
The information displayed by the shell prompt can vary. This depends on your linux distribution and you personal settings ( environment).
Now, it is time to start explore what is inside your home directory and where exactly is your home directory located. For this exploration we need some tools. One such a tool can be ls command which has an ability to display a list of files and directories.
well it seems that something went wrong because no output was produced on the screen. This is perfectly normal output if there are no files or directories in your current directory. Current directory means a directory where you are located and any given time, at the moment is is home directory. Default behaviour of the ls command is to show list of files and directories of current directory. Command pwd will show your current location:
Default behaviour of ls command can be overridden by entering some options and arguments to ls command itself. So, for instance if we want to list all files and directories in / ( root directory ) we would enter / as a argument to ls.
linuxconfig.org:~$ ls /
bin cdrom etc initrd lib media opt root srv tmp var
boot dev home initrd.img lost+found mnt proc sbin sys usr vmlinuz
In the command above we have supplied an argument to ls command which is a directory location / . / ( root directory ) is a parent directory of all other directories within a filesystem. The output therefore shows all directories and files located under / directory. Please note that our /home directory is also located there.
Let us create some files that we have more data to play with. The next command will create 2 files: foo and .bar. The difference between those two files is that file .bar is a hidden file and the file foo is not. Any directory and file which has a dot in front of its name is hidden. To create these two files we use a touch command.
linuxconfig.org:~$ touch foo .bar
Now we can use our ls command again to confirm that files had been created:
Just because the .bar file is hidden it will be not shown when ls command is executed. Ls command does not show hidden files by default and therefore we need to override its behaviour with -a option.
linuxconfig.org:~$ ls -a
. .. .bar .bash_profile .bashrc foo
Option -a causes ls command to list all files including hidden files. Note that we have couple other hidden files in your home directory. What is important to notice at this point is that previously created file .bar is listed in the ls output.
Using a -l option combined with ls command will give us more detailed information about any given file or directory. Consider a following example:
linuxconfig.org:~$ ls -l foo
-rw-r--r-- 1 linuxconfig linuxconfig 0 2008-01-13 00:52 foo
when -l options ( long listing format ) and foo argument is supplied to ls command, the ls command displays detailed information about the foo file. This information includes file permission, owner, group, size, creation date +time and lastly file name.
Another useful option to modify default behaviour of ls command is -h. This option will display size of the in a human readable format. By default ls -l command will display file size in bytes:
linuxconfig.org:~$ ls -l /etc/services
-rw-r--r-- 1 root root 18274 2007-02-02 13:09 /etc/services
We can combine two options -lh to get the previous output in KB:
linuxconfig.org:~$ ls -lh /etc/services
-rw-r--r-- 1 root root 18K 2007-02-02 13:09 /etc/services
As it was already mentioned above a ls command without any arguments will display list of files and directories in the current directory. There may be some case that we need to list files a directories in all subdirectories. The following ls command with an option -R will recursively list all files. First we need to create two directories with mkdir command: dir1 and dir2 where dir1 will be a parent directory of dir2. Then we will create a file called foobar inside a dir2 and use ls -R to list recursively current directory:
linuxconfig.org:~$ mkdir -p dir1/dir2
linuxconfig.org:~$ touch dir1/foobar
linuxconfig.org:~$ ls -R
Output analysis will reveal the following findings:
ls [OPTION]... [FILE]...
-l use a long listing format
do not ignore entries starting with
-t sort by modification time
reverse order while sorting
with -l, print sizes in human readable format (e.g., 1K 234M 2G)
cd, cat, pwd