Professional Documents
Culture Documents
2.5 Working With Files and Directories
2.5 Working With Files and Directories
Open Transcript
In this topic, the commands to work with files and directories in Linux will be discussed.
touch
cisco@cisco: ~$
cisco@cisco: ~$ touch catalyst_config.txt
cisco@cisco: ~$
cisco@cisco: ~$ ls
catalyst_config.txt cisco Desktop Documents Downloads Templates
cisco@cisco: ~$
cisco@cisco: ~$ ls -l catalyst_config.txt
-rw-rw-r-- 1 cisco cisco 0 Jun 24 11:50 catalyst_config.txt
To create an empty file, use the touch command, which creates a new, empty file without automatically
overwriting existing files with the same name. Also, it updates the timestamp of a file.
A common example would be when creating a new configuration text file but with no contents yet. Use the
command:
touch config.txt
This command creates the file config.txt in the current working directory. Another way to create the same file in
the current working directory is to identify the actual current working directory with (./).
touch ./config.txt
The mkdir command will simply create a new directory. However, when creating a new directory that requires
other new directories in the path to also be created, use the mkdir -p command. For example, if you execute
the command mkdir /cisco/platforms/data center/nexus , and all directories between your present working
directory and nexus have not been previously created, the command will fail. If you execute mkdir –p
/cisco/platforms/data center/nexus , all the required directories will be created including the directory nexus.
Remove Commands
Command Description
rm Removes a file
In order to remove (delete) a file, use the rm command followed by the filename to be deleted. In order to remove
a directory, use the rmdir command, however, the directory must be empty in order to be deleted in this way. In
order to remove an entire directory tree, use rm -r .
Command Description
cp Copy a file
Command Description
mv Move/rename a file
cisco@cisco: ~/Nexus9000$
cisco@cisco: ~/Nexus9000$ cp file1 file2
cisco@cisco: ~/Nexus9000$
cisco@cisco: ~/Nexus9000$ mv file2 file3
cisco@cisco: ~/Nexus9000$
cisco@cisco: ~/Nexus9000$ ls
file1 file3
In order to copy a file, use the cp command along with the file’s source and destination. To copy an entire
directory, use cp with the -r option.
To move a file from one location to another in the filesystem, use the mv command along with the files source and
destination.
For example, to move a file that is called nexus.txt from its current directory to a directory called Testing, the
syntax would be:
If the file is not in your working directory, you can identify the source directory as well using the following syntax:
To rename a file, the same command is used, but the destination of the file is the same as its source directory. The
syntax to rename a file without moving it would be:
There are several commands that allow you to view the contents of a file, but each provides a slightly different
functionality.
The more command allows you to view the contents of a file one page at a time and requires you to press the
spacebar to advance to the next page of contents.
The less command also allows you to view the contents of the file but it allows the user the ability to scroll up
and down and perform a keyword search.
The cat command (short for concatenate) is used to stream the entire contents of the file without pausing. It is
useful for files with just a few lines. Also, cat is used to add contents to a file.
For example, to stream to the terminal all the contents of a router configuration file that is located in the current
working directory and called router1.txt, enter
If the file is located in a different directory, simply include the path in the command. For example, if the router1.txt
file is in /cisco/platforms/routers, enter
Another use for the cat command is to add the contents of one file to the contents of another. For example, to
add the contents of the file router2.txt to the file router1.txt, the syntax would be:
There are several options to search within files. The head command by default displays the first 10 lines. The
tail command displays the last 10 lines of a file.
The diff command in Linux is used to compare files line by line to check for difference. It is a very popular
command to use especially for network engineers comparing a current configuration with a previous configuration
to check for changes.
Two files:
vlan 10
cisco@cisco:~$
-vlan 50
+vlan 10
File Permissions
This topic examines the various file permissions that are used within the Linux system.
Because it is a multi-user operating system, Linux has a built-in permission mechanism to prevent one user from
viewing, modifying or removing another user’s files.
In order to understand file permissions, you can look at the permissions from the output of a ls command. This
time using the –l flag so you can look at and view permissions, but also see who the owner of the file and group
they are in.
cisco@cisco:~$ ls -l vlans.cfg
-rw-rw-r-- 1 cisco cisco 117 Aug 6 2015 vlans.cfg
cisco@cisco:~$
Based on the line that is shown, you can see the following:
All users in the cisco group can read and write to the file.
cisco@cisco:~$ ls -l vlans_script.py
-rw-rw-r-- 1 cisco cisco 0 Sep 12 15:14 vlans_script.py
cisco@cisco:~$
cisco@cisco:~$ chmod u+x vlans_script.py
cisco@cisco:~$
cisco@cisco:~$ ls -l vlans_script.py
-rwxrw-r-- 1 cisco cisco 0 Sep 12 15:14 vlans_script.py
cisco@cisco:~$
cisco@cisco:~$ chmod go+x+w vlans_script.py
cisco@cisco:~$
cisco@cisco:~$ ls -l vlans_script.py
-rwxrwxrwx 1 cisco cisco 0 Sep 12 15:14 vlans_script.py
cisco@cisco:~$
In order to update and change file permissions, you can use the chmod command. The format of the command is
as follows:
chmod u+x vlans_script.py says that the user (cisco, in this case) of the file should be able to eXecute the
script.
chmod go+x+w vlans_sript.py says that the group (g) and other users (o) should be able to read and write to
the script file.
Changing Permissions
VALUE MEANING
The file's owner may read, write, and execute the file.
u+rwx,go+rx,go-x
All others may read and execute the file.
The file's owner may read, write, and execute the file.
u+rwx,og-rwx
Nobody else has any rights.
The owner may read and write a file, while all others
u+rw,u-x,go+r,go-wx
may only read the file.
The owner may read and write a file. All others have
u+rw,u-x,go-rwx
no rights.
Which command would be correct to move a file that is called class.txt from its current directory to a
directory called MyClass?
Submit