Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

2.

5 Linux Primer for Network Engineers

Working with Files and Directories

Open Transcript

In this topic, the commands to work with files and directories in Linux will be discussed.

touch

Updated time stamps of files and creates an “empty” file

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

Make Directory Commands


Command Description

mkdir Make a new directory

mkdir -p Make all required sub-directories in the path

To create new directories, use the mkdir command.

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

rm -r Removes an entire directory and its contents

rmdir Removes an entire empty directory

Removes a directory and contents including write-


rm -rf
protected files

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 .

Copy and Move Commands

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:

cisco@cisco: ~$ mv nexus.txt Testing

If the file is not in your working directory, you can identify the source directory as well using the following syntax:

cisco@cisco: ~$ mv nexus.txt home/cisco/nexus home/cisco/Testing/

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:

cisco@cisco: ~$ mv nexus.cfg nexus123.cfg

Viewing Files Commands


Command Description

similar to using Cisco CL—space bar takes you down


more
a full screen length (% in bottom left).

“less is more” because it allows the user to scroll up


less and down using arrow keys vs. just the ability to space
down.

cat Streams the file top to bottom without pausing.

head By default shows first 10 lines of a file.


Command Description

tail By default shows last 10 lines of a file.

diff View diff between two files (hint: use –c option).

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

cisco@cisco: ~$ cat router1.txt

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

cisco@cisco: ~$ cat cisco/platforms/routers/router1.txt

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:

cisco@cisco: ~$ cat router2.txt >> router1.txt

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.

Here is an example using the diff command:

Two files:

cisco@cisco:~$ more test.cfg


username cisco password cisco
username sjc password sjc
vlan 50
cisco@cisco:~$
cisco@cisco:~$ more test_new.cfg
username cisco password cisco
username sjc password nyc

vlan 10
cisco@cisco:~$

Viewing the diffs:

cisco@cisco:~$ diff -u test.cfg test_new.cfg


--- test.cfg 2016-09-12 07:53:04.487466471 -0700
+++ test_new.cfg 2016-09-12 07:53:03.076172461 -0700
@@ -1,4 +1,4 @@
username cisco password cisco
-username sjc password sjc
+username sjc password nyc

-vlan 50
+vlan 10

File Permissions
This topic examines the various file permissions that are used within the Linux system.

Linux operating systems are multi-user

Permissions are based on two factors:

Permissions assigned to a specific user and group

Permissions assigned to a specific action (read, write, execute)

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.

Linux permissions are based on two factors:

Permissions that are based on specific user or group

Permissions that are based on specific user or group


cisco@cisco:~$ ls -l vlans.cfg
-rw-rw-r-- 1 cisco cisco 117 Aug 6 2015 vlans.cfg
cisco@cisco:~$

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.

Take note of the following example:

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:

vlans.cfg is a file (from the first character in the line).

The cisco user can read and write to the file.

All users in the cisco group can read and write to the file.

All others can only read 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, g, o] (+/- [r, w, x]) for each action

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

rwx+ugo No restrictions on permissions.

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.

ugo+rw,ugo-w All users may read and write the file.

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.

Content Review Question

Which command would be correct to move a file that is called class.txt from its current directory to a
directory called MyClass?

cisco@cisco: ~$ mv MyClass class.txt

cisco@cisco: ~$ mv class.txt MyClass

cisco@cisco: ~$ move class.txt MyClass


cisco@cisco: ~$ move MyClass class.txt

Submit

You might also like