In the earlier post, we saw the Linux directory structure, today we will see how to navigate within directories and view contents.
- The Current Working Directory:
Every shell or system process has a current working directory which can be seen with the command “pwd“.
So when so change user to something that an application is using or when you login to the system, you have land inside a directory which is assigned to you. This will be your present or current working directory, unless you choose to move somewhere else.
When you move to another directory that new directory becomes your present working directory.
You can change or move from one directory to another with the “cd” command.
# cd /home/user1/projects - File and Directory names:
The names of files and directories can be upto 255 characters, all characters are valid except for “/“.
Names are case sensitive. - Pathnames:
There are 2 types of pathnames:
1. Absolute Pathnames.
2. Relative Pathnames.
Absolute Pathnames: Absolute Pathnames begins with a forward slash “/“. It is a complete path or roadmap to a file location and can be used from anywhere regardless of your present working directory.
# ll /etc/httpd/httpd.conf
So httpd.conf is the file name whereas /etc/httpd is the file location, so pathname is the combination of both filename and the file location.
Relative Pathname: Relative pathnames do not begin with “/” and can be used to refer to a file or directory only in your present directory.
# ll httpd.conf
# pwd
/etc/httpd - Changing Directories:
You can change or move from one directory to another with the “cd” command.
# cd /home/user1/projects
To navigate within your present working directory you can use relative pathnames, however to navigate to some directory outside your pwd, you have to mention the absolute pathname.
To go up one directory level:
# cd ..
If you want to go further up keep adding /.. so
# cd ../..
will take you 2 directories up.
To go back to your home directory issue the cd command without any parameter:
# cd
To move back to the earlier directory:
# cd - - Listing Directories:
To list the contents of the current directories or any specified directory, the command is basically "ls"
We do not have anything in the directory so it didn’t list anything.
“ls” takes different flags, some of which we will see now.
To list files including hidden file:
# ls -a
To display extra information about the contents:
# ls -al
As you can see this command list the files along with permissions, owner, size and creation date
To see recursive listing:
# ls -R
As you see with “R” flag you can see within the cat directories as well.
To see the files in reverse order you may you the “r” flag:
# ls -r
As you can see when you used the “r” flag the listing was reversed.
This flag is to be used along with “l” flag.
To sort by the modification time we can use the “t” flag:
# ls -lt
As you can see in the first snap the listing is dog and then cat, but after the modification time changed for the cat directory, the listing changed and cat came on top.
This flag has to be used with the “l” flag.
To see the size of the directory or the file in human readable format, you can use the “h” flag:
#ls -lh
You can see that instead of 4096 its now showing 4k. Again this flag is to be used with the “l” flag.
To see the inode details of the contents use the “i” flag:
#ls -li
You can use this with or without the “l” option. It looks good with “l” though.
We will see what inodes are in details later.
You may very well combine all options together:
#ls -lrthai
"ls" has a lot more flags, I have listed only the ones which is mostly used, there are flags to “sort“, “format“, etc. To know and explore more on it make sure to go through the man page of “ls“
To do so, just use the below command:
#man ls
Remember in Linux when you want to explore any command man pages are the way to go, so spend more time reading and exploring the man pages and get familiar with it.
That’s it for now, see you in the next post of blog.avoidingtech.com