Navigating the File System
Let's learn some commands on navigating the file system.
The LS command
ls
- lists the contents of the current directory or can also be used to list the contents of any other directory.
By default, the list is sorted alphabetically by name.
e.g. ls /
will list the contents of the root directory (/ indicates the root directory)
ls can be used to list the contents of multiple directories, by separating each directory with a space.
e.g. ls ~ Sweta
will list the contents of my home directory (indicated by ~) and that of user Sweta. Note what the output looks like
/home/me:
Aditya nick ranin Shraddha Sweta
Sweta:
CAT NOD
Options
Note that some commands in Linux support options that alter the behavior of the command. We will discuss the options with the list (ls) command below.
-l
can be used to display the output in a long format so that you can view more details about the directories and files. See what the output looks like below :
total 20
drwxrwxr-x 2 app_etlmgmt_qa app_etlmgmt_qa 4096 Aug 18 15:00 Aditya
drwxrwxr-x 2 app_etlmgmt_qa app_etlmgmt_qa 4096 Aug 11 10:08 nick
drwxrwxr-x 4 app_etlmgmt_qa app_etlmgmt_qa 4096 Aug 15 11:25 ranin
drwxrwxr-x 3 app_etlmgmt_qa app_etlmgmt_qa 4096 Aug 17 16:15 Shraddha
drwxrwxr-x 4 app_etlmgmt_qa app_etlmgmt_qa 4096 Aug 11 14:05 Sweta
will display the contents of the current working directory, in a long (l) format including permissions, owners, groups, size and creation date of files and folders, sorted by the file's modification time (t) in reverse order (--reverse).
e.g. ls -l ./
total 8
drwxr-xr-x. 2 user user 6 Jan 7 2015
Desktop
-rw-rw-r--. 1 user user 431 Aug 6 2015 VNCHOWTO
-rw-------. 1 user user 68 Mar 18 2016 xrdp-chansrv.log
Note we have the permissions, owners, groups, size and creation date of each of the files and folders in the list.
Compare the above output to only the 'ls' command
$ ls ./
Desktop VNCHOWTO xrdp-chansrv.log
-a / --all
List all files, even those with names that begin with a period, which are normally not listed (i.e., hidden).
-R
The capital R flag is used to list the contents of a directory recursively such that all files under the current folder and under all the subfolders are listed.
In order to test how the recursive command works, I created a folder structure like this with some files under it. Note directories are indicated by the - sign.
-rbTest1
rbTestFile
-rbTest2
rbTestFile2
-rbTest21
rbTestFile21
Now when I use the -R option (along with the -p option so that I can easily identify directories), this is the result I get :
$ ls -pR
.:
Desktop/ rbTest1/ rbTest2/ VNCHOWTO xrdp-chansrv.log
./Desktop:
./rbTest1:
rbTestFile
./rbTest2:
rbTest21/ rbTestFile2
./rbTest2/rbTest21:
rbTestFile21
Note how you can see the entire structure of the directory from the current directory all the way until the bottom level.
Wildcards
Note you can use wildcards with the ls command to filter the list of files and folders to those whose names match a specific pattern. For e.g. if you only wanted to list the contents of only those files and folders that begin with the pattern "rb", you would use the command: ls rb*
-S
Sort results by file size.
-t
Sort by modification time.
-p
This option appends an indicator to folders (the indicator is a forward slash /).
e.g. $ ls
Output : Desktop VNCHOWTO xrdp-chansrv.log
In the results above, we can't tell which of the items are files and which ones are folders.
$ ls -p
Output : Desktop/ VNCHOWTO xrdp-chansrv.log
Now with the -p option, the "Desktop" item has a forward slash appended to it, indicating that it is a folder. The rest of the items are files.
Similarly some command options use the entire word spelled out preceeded by two dashes e.g. ls --reverse will reverse the order of the sort. Also commands support multiple options to be strung together e.g. ls -lt where the t options sorts the output by the file's modification time.
ls -lt --reverse
Will display the list of files and folders sorted in reverse order by file's modification time
-h / --human-readable
In long format listings, displays file sizes in human-readable format rather than in bytes.
Note the difference in the output between ls -lh and ls -l
ls -lh
drwxrwxr-x 2 app_etlmgmt_qa app_etlmgmt_qa4.0KAug 18 15:00 Aditya
drwxrwxr-x 2 app_etlmgmt_qa app_etlmgmt_qa4.0KAug 11 10:08 nick
ls -l
drwxrwxr-x 2 app_etlmgmt_qa app_etlmgmt_qa4096Aug 18 15:00 Aditya
drwxrwxr-x 2 app_etlmgmt_qa app_etlmgmt_qa4096Aug 11 10:08 nick
The File command
To get more information about a file's type, we use the file command
e.g. file test.jpg
could give you the following output :
test.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 1280x5859, frames 3
The PWD command
pwd
- prints the name of the current working directory
The CD command
cd
- used to change the directory
cd can also be used to navigate up the directory structure using relative paths. cd .. takes you to the parent directory of your current directory cd ../../ takes you two levels up. A sngle dot . represents the current directory
Note that as you change your current directory, the shell prompt changes accordingly to show your current working directory.
Some shortcuts to change the current working directory:
cd | Changes the working directory to your home directory |
---|---|
cd - | Changes the working directory to the previous working directory |
cd ~ username | Changes the working directory to the home directory of the specific user indicated by username. e.g. cd ~ bob would change the working directory to user bob's home directory. |
IMPORTANT FACTS ABOUT FILENAMES
Filenames that begin with a period character are hidden. This only means that
ls
will not list them unless you sayls -a
. When your account was created, several hidden files were placed in your home directory to configure things for your account. Later on we will take a closer look at some of those files to see how you can customize your environment. In addition, some applications place their configuration and settings files in your home directory as hidden files.Filenames and commands in Linux, as in Unix, are case sensitive. The filenames_File1_and_file1_refer to different files.
Linux has no concept of a “file extension” like some other operating systems. You may name files any way you like. The contents and/or purpose of a file is determined by other means. Although Unix-like operating systems don’t use file extensions to determine the contents/purpose of files, some application programs do.
Though Linux supports long filenames that may contain embedded spaces and punctuation characters, limit the punctuation characters in the names of files you create to period, dash (hyphen), and underscore.Most importantly, do not embed spaces in filenames. Embedding spaces in filenames will make many command line tasks more difficult
The CP command
This is the Copy command.
The cp command copies files or directories. It can be used two different ways:
cp item1 item2
to copy the single file or directory item1 to file or directory item2 and:
cp item... directory
to copy multiple items (either files or directories) into a directory.
Some common options used with the copy command (with the long and short equivalent) :
-a, --archive
Copy the files and directories and all of their attributes, including ownerships and permissions. Normally, copies take on the default attributes of the user performing the copy.
-i, --interactive
Before overwriting an existing file, prompt the user for confirmation. If this option is not specified, cp will silently overwrite files.
-r, --recursive
Recursively copy directories and their contents. This option (or the -a option) is required when copying directories.
-u, --update
When copying files from one directory to another, copy only files that either don’t exist or are newer than the existing corresponding files in the destination directory.
-v, --verbose
Display informative messages as the copy is performed.
Some examples:
cp file1 file2
Copy file1 to file2. If file2 exists, it is overwritten with the contents of file1. If file2 does not exist, it is created.
cp -i file1 file2
Same as above, except that if file2 exists, the user is prompted before it is overwritten.
cp file1 file2 dir1
Copy file1 and file2 into directory dir1. dir1 must already exist.
cp dir1/* dir2
Using a wildcard, all the files in dir1 are copied into dir2. dir2 must already exist.
cp -r dir1 dir2
Copy directory dir1 (and its contents) to directory dir2. If directory dir2 does not exist, it is created and will contain the same contents as directory dir1.