Linux Basic
Linux Basic
net/publication/352121769
Linux Basics!
CITATIONS READS
0 610
1 author:
Naol Getachew
Mattu University
27 PUBLICATIONS 0 CITATIONS
SEE PROFILE
All content following this page was uploaded by Naol Getachew on 04 June 2021.
Page 1
working with directories
pwd
The you are here sign can be displayed with the pwd command (Print Working Directory).
Go ahead, try it: Open a command line interface (also called a terminal, console or xterm)
and type pwd. The tool displays your current directory.
naol@admin:~$ pwd
/home/naol
cd
You can change your current directory with the cd command (Change Directory).
naol@admin$ cd /etc
naol@admin$ pwd
/etc
naol@admin$ cd /bin
naol@admin$ pwd
/bin
naol@admin$ cd /home/naol/
naol@admin$ pwd
/home/naol
cd ~
The cd is also a shortcut to get back into your home directory. Just typing cd without a target
directory, will put you in your home directory. Typing cd ~ has the same effect.
naol@admin$ cd /etc
naol@admin$ pwd
/etc
naol@admin$ cd
naol@admin$ pwd
/home/naol
naol@admin$ cd ~
naol@admin$ pwd
/home/naol
cd ..
To go to the parent directory (the one just above your current directory in the directory
tree), type cd .. .
naol@admin$ pwd
/usr/share/games
naol@admin$ cd ..
naol@admin$ pwd
/usr/share
To stay in the current directory, type cd . ;-) We will see useful use of the . character
representing the current directory later.
Page 2
working with directories
cd -
Another useful shortcut with cd is to just type cd - to go to the previous directory.
naol@admin$ pwd
/home/naol
naol@admin$ cd /etc
naol@admin$ pwd
/etc
naol@admin$ cd -
/home/naol
naol@admin$ cd -
/etc
When inside /home, you have to type cd naol instead of cd /naol to enter the subdirectory
naol of the current directory /home.
naol@admin$ pwd
/home
naol@admin$ cd /naol
bash: cd: /naol: No such file or directory
naol@admin$ cd naol
naol@admin$ pwd
/home/naol
In case your current directory is the root directory /, then both cd /home and cd home will
get you in the /home directory.
naol@admin$ pwd
/
naol@admin$ cd home
naol@admin$ pwd
/home
naol@admin$ cd /
naol@admin$ cd /home
naol@admin$ pwd
/home
This was the last screenshot with pwd statements. From now on, the current directory will
often be displayed in the prompt. Later in this book we will explain how the shell variable
$PS1 can be configured to show this.
Page 3
working with directories
path completion
The tab key can help you in typing a path without errors. Typing cd /et followed by the tab
key will expand the command line to cd /etc/. When typing cd /Et followed by the tab key,
nothing will happen because you typed the wrong path (upper case E).
You will need fewer key strokes when using the tab key, and you will be sure your typed
path is correct!
ls
You can list the contents of a directory with ls.
naol@admin:~$ ls
allfiles.txt dmesg.txt services stuff summer.txt
naol@admin:~$
ls -a
A frequently used option with ls is -a to show all files. Showing all files means including
the hidden files. When a file name on a Linux file system starts with a dot, it is considered
a hidden file and it doesn't show up in regular file listings.
naol@admin:~$ ls
allfiles.txt dmesg.txt services stuff summer.txt
naol@admin:~$ ls -a
. allfiles.txt .bash_profile dmesg.txt .lesshst stuff
.. .bash_history .bashrc services .ssh summer.txt
naol@admin:~$
ls -l
Many times you will be using options with ls to display the contents of the directory in
different formats or to display different parts of the directory. Typing just ls gives you a
list of files in the directory. Typing ls -l (that is a letter L, not the number 1) gives you a
long listing.
naol@admin:~$ ls -l
total 17296
-rw-r--r-- 1 naol naol 17584442 Sep 17 00:03 allfiles.txt
-rw-r--r-- 1 naol naol 96650 Sep 17 00:03 dmesg.txt
-rw-r--r-- 1 naol naol 19558 Sep 17 00:04 services
drwxr-xr-x 2 naol naol 4096 Sep 17 00:04 stuff
-rw-r--r-- 1 naol naol 0 Sep 17 00:04 summer.txt
Page 4
working with directories
ls -lh
Another frequently used ls option is -h. It shows the numbers (file sizes) in a more human
readable format. Also shown below is some variation in the way you can give the options
to ls. We will explain the details of the output later in this book.
Note that we use the letter L as an option in this screenshot, not the number 1.
naol@admin:~$ ls -l -h
total 17M
-rw-r--r-- 1 naol naol 17M Sep 17 00:03 allfiles.txt
-rw-r--r-- 1 naol naol 95K Sep 17 00:03 dmesg.txt
-rw-r--r-- 1 naol naol 20K Sep 17 00:04 services
drwxr-xr-x 2 naol naol 4.0K Sep 17 00:04 stuff
-rw-r--r-- 1 naol naol 0 Sep 17 00:04 summer.txt
naol@admin:~$ ls -lh
total 17M
-rw-r--r-- 1 naol naol 17M Sep 17 00:03 allfiles.txt
-rw-r--r-- 1 naol naol 95K Sep 17 00:03 dmesg.txt
-rw-r--r-- 1 naol naol 20K Sep 17 00:04 services
drwxr-xr-x 2 naol naol 4.0K Sep 17 00:04 stuff
-rw-r--r-- 1 naol naol 0 Sep 17 00:04 summer.txt
naol@admin:~$ ls -hl
total 17M
-rw-r--r-- 1 naol naol 17M Sep 17 00:03 allfiles.txt
-rw-r--r-- 1 naol naol 95K Sep 17 00:03 dmesg.txt
-rw-r--r-- 1 naol naol 20K Sep 17 00:04 services
drwxr-xr-x 2 naol naol 4.0K Sep 17 00:04 stuff
-rw-r--r-- 1 naol naol 0 Sep 17 00:04 summer.txt
naol@admin:~$ ls -h -l
total 17M
-rw-r--r-- 1 naol naol 17M Sep 17 00:03 allfiles.txt
-rw-r--r-- 1 naol naol 95K Sep 17 00:03 dmesg.txt
-rw-r--r-- 1 naol naol 20K Sep 17 00:04 services
drwxr-xr-x 2 naol naol 4.0K Sep 17 00:04 stuff
-rw-r--r-- 1 naol naol 0 Sep 17 00:04 summer.txt
naol@admin:~$
Page 5
working with directories
mkdir
Walking around the Unix file tree is fun, but it is even more fun to create your own directories
with mkdir. You have to give at least one parameter to mkdir, the name of the new directory
to be created. Think before you type a leading / .
naol@admin:~$ mkdir mydir
naol@admin:~$ cd mydir
naol@admin:~/mydir$ ls -al
total 8
drwxr-xr-x 2 naol naol 4096 Sep 17 00:07 .
drwxr-xr-x 48 naol naol 4096 Sep 17 00:07 ..
naol@admin:~/mydir$ mkdir stuff
naol@admin:~/mydir$ mkdir otherstuff
naol@admin:~/mydir$ ls -l
total 8
drwxr-xr-x 2 naol naol 4096 Sep 17 00:08 otherstuff
drwxr-xr-x 2 naol naol 4096 Sep 17 00:08 stuff
naol@admin:~/mydir$
mkdir -p
The following command will fail, because the parent directory of threedirsdeep does not
exist.
naol@admin:~$ mkdir mydir2/mysubdir2/threedirsdeep
mkdir: cannot create directory ‘mydir2/mysubdir2/threedirsdeep’: No such fi\
le or directory
When given the option -p, then mkdir will create parent directories as needed.
naol@admin:~$ mkdir -p mydir2/mysubdir2/threedirsdeep
naol@admin:~$ cd mydir2
naol@admin:~/mydir2$ ls -l
total 4
drwxr-xr-x 3 naol naol 4096 Sep 17 00:11 mysubdir2
naol@admin:~/mydir2$ cd mysubdir2
naol@admin:~/mydir2/mysubdir2$ ls -l
total 4
drwxr-xr-x 2 naol naol 4096 Sep 17 00:11 threedirsdeep
naol@admin:~/mydir2/mysubdir2$ cd threedirsdeep/
naol@admin:~/mydir2/mysubdir2/threedirsdeep$ pwd
/home/naol/mydir2/mysubdir2/threedirsdeep
rmdir
When a directory is empty, you can use rmdir to remove the directory.
naol@admin:~/mydir$ ls -l
total 8
drwxr-xr-x 2 naol naol 4096 Sep 17 00:08 otherstuff
drwxr-xr-x 2 naol naol 4096 Sep 17 00:08 stuff
naol@admin:~/mydir$ rmdir otherstuff
naol@admin:~/mydir$ cd ..
naol@admin:~$ rmdir mydir
rmdir: failed to remove ‘mydir’: Directory not empty
naol@admin:~$ rmdir mydir/stuff
naol@admin:~$ rmdir mydir
naol@admin:~$
Page 6
working with directories
rmdir -p
And similar to the mkdir -p option, you can also use rmdir to recursively remove
directories.
naol@admin:~$ mkdir -p test42/subdir
naol@admin:~$ rmdir -p test42/subdir
naol@admin:~$
Page 7
working with directories
Now change to your home directory using only three key presses.
10. Stay where you are, and list the contents of /bin and /sbin.
12. List all the files (including hidden files) in your home directory.
15. Change to the /etc directory, stay here and create a directory newdir in your home
directory.
16. Create in one command the directories ~/dir1/dir2/dir3 (dir3 is a subdirectory from dir2,
and dir2 is a subdirectory from dir1 ).
17. Remove the directory testdir.
Page 8
working with directories
Now change to your home directory using only three key presses.
cd (and the enter key)
10. Stay where you are, and list the contents of /bin and /sbin.
ls /bin /sbin
12. List all the files (including hidden files) in your home directory.
ls -al ~
15. Change to the /etc directory, stay here and create a directory newdir in your home
directory.
Page 9
working with directories
16. Create in one command the directories ~/dir1/dir2/dir3 (dir3 is a subdirectory from dir2,
and dir2 is a subdirectory from dir1 ).
mkdir -p ~/dir1/dir2/dir3
Page 10
Lab manual: working with files
In this Lab manual we learn how to recognise, create, remove, copy and move files using
commands like file, touch, rm, cp, mv and rename.
Page 11
working with files
everything is a file
A directory is a special kind of file, but it is still a (case sensitive!) file. Each terminal
window (for example /dev/pts/4), any hard disk or partition (for example /dev/sdb1) and
any process are all represented somewhere in the file system as a file. It will become clear
throughout this course that everything on Linux is a file.
file
The file utility determines the file type. Linux does not use extensions to determine the
file type. The command line does not care whether a file ends in .txt or .pdf. As a system
administrator, you should use the file command to determine the file type. Here are some
examples on a typical Linux system.
naol@laika:~$ file pic3png
pic3png: PNG image data, 3840 x 1200, 8-bit/color RGBA, non-interlaced
naol@laika:~$ file /etc/passwd
/etc/passwd: ASCII text
naol@laika:~$ file HelloWorld.c
HelloWorld.c: ASCII C program text
The file command uses a magic file that contains patterns to recognise file types. The magic
file is located in /usr/share/file/magic. Type man 5 magic for more information.
It is interesting to point out file -s for special files like those in /dev and /proc.
root@admin~# file /dev/sda
/dev/sda: block special
root@admin~# file -s /dev/sda
/dev/sda: x86 boot sector; partition 1: ID=0x83, active, starthead...
root@admin~# file /proc/cpuinfo
/proc/cpuinfo: empty
root@admin~# file -s /proc/cpuinfo
/proc/cpuinfo: ASCII C++ program text
Page 12
working with files
touch
create an empty file
One easy way to create an empty file is with touch. (We will see many other ways for
creating files later in this book.)
This screenshot starts with an empty directory, creates two files with touch and the lists
those files.
naol@admin:~$ ls -l
total 0
naol@admin:~$ touch file42
naol@admin:~$ touch file33
naol@admin:~$ ls -l
total 0
-rw-r--r-- 1 naol naol 0 Oct 15 08:57 file33
-rw-r--r-- 1 naol naol 0 Oct 15 08:56 file42
naol@admin:~$
touch -t
The touch command can set some properties while creating empty files. Can you determine
what is set by looking at the next screenshot? If not, check the manual for touch.
naol@admin:~$ touch -t 200505050000 Myfile
naol@admin:~$ touch -t 130207111630 BigBattle.txt
naol@admin:~$ ls -l
total 0
-rw-r--r-- 1 naol naol 0 Jul 11 1302 BigBattle.txt
-rw-r--r-- 1 naol naol 0 Oct 15 08:57 file33
-rw-r--r-- 1 naol naol 0 Oct 15 08:56 file42
-rw-r--r-- 1 naol naol 0 May 5 2005 Myfile
naol@admin:~$
Page 13
working with files
rm
remove forever
When you no longer need a file, use rm to remove it. Unlike some graphical user interfaces,
the command line in general does not have a waste bin or trash can to recover files. When
you use rm to remove a file, the file is gone. Therefore, be careful when removing files!
naol@admin:~$ ls
BigBattle.txt file33 file42 Myfile
naol@admin:~$ rm BigBattle.txt
naol@admin:~$ ls
file33 file42 Myfile
naol@admin:~$
rm -i
To prevent yourself from accidentally removing a file, you can type rm -i.
naol@admin:~$ ls
file33 file42 Myfile
naol@admin:~$ rm -i file33
rm: remove regular empty file `file33'? yes
naol@admin:~$ rm -i Myfile
rm: remove regular empty file `Myfile'? n
naol@admin:~$ ls
file42 Myfile
naol@admin:~$
rm -rf
By default, rm -r will not remove non-empty directories. However rm accepts several
options that will allow you to remove any directory. The rm -rf statement is famous because
it will erase anything (providing that you have the permissions to do so). When you are
logged on as root, be very careful with rm -rf (the f means force and the r means recursive)
since being root implies that permissions don't apply to you. You can literally erase your
entire file system by accident.
naol@admin:~$ mkdir test
naol@admin:~$ rm test
rm: cannot remove `test': Is a directory
naol@admin:~$ rm -rf test
naol@admin:~$ ls test
ls: cannot access test: No such file or directory
naol@admin:~$
Page 14
working with files
cp
copy one file
To copy a file, use cp with a source and a target argument.
naol@admin:~$ ls
file42 Myfile
naol@admin:~$ cp file42 file42.copy
naol@admin:~$ ls
file42 file42.copy Myfile
cp -r
To copy complete directories, use cp -r (the -r option forces recursive copying of all files
in all subdirectories).
naol@admin:~$ ls
dir42 file42 file42.copy Myfile
naol@admin:~$ cp -r dir42/ dir33
naol@admin:~$ ls
dir33 dir42 file42 file42.copy Myfile
naol@admin:~$ ls dir33/
Myfile
4.6.5. cp -i
To prevent cp from overwriting existing files, use the -i (for interactive) option.
naol@admin:~$ cp Myfile file42
naol@admin:~$ cp Myfile file42
naol@admin:~$ cp -i Myfile file42
cp: overwrite `file42'? n
naol@admin:~$
Page 15
working with files
4.7. mv
4.7.1. rename files with mv
Use mv to rename a file or to move the file to another directory.
naol@admin:~$ ls
dir33 dir42 file42 file42.copy Myfile
naol@admin:~$ mv file42 file33
naol@admin:~$ ls
dir33 dir42 file33 file42.copy Myfile
naol@admin:~$
When you need to rename only one file then mv is the preferred command to use.
4.7. mv -i
The mv also has a -i switch similar to cp and rm.
this screenshot shows that mv -i will ask permission to overwrite an existing file.
naol@admin:~$ mv -i file33 Myfile
mv: overwrite `Myfile'? no
naol@admin:~$
Page 16
working with files
rename
about rename
The rename command is one of the rare occasions where the Linux Fundamentals book
has to make a distinction between Linux distributions. Almost every command in the
Fundamentals part of this book works on almost every Linux computer. But rename is
different.
Try to use mv whenever you need to rename only a couple of files.
The rename command on Ubuntu uses regular expressions (regular expression or shor regex
are explained in a later lab manual) to rename many files at once.
Below a rename example that switches all occurrences of txt to png for all file names ending
in .txt.
naol@admin:~/test42$ ls
abc.txt file3txt file42.txt
naol@admin:~/test42$ rename 's/\.txt/\.png/' *.txt
naol@admin:~/test42$ ls
abc.png file3png file42.png
This second example switches all (first) occurrences of file into document for all file names
ending in .png.
naol@admin:~/test42$ ls
abc.png file3png file42.png
naol@admin:~/test42$ rename 's/file/document/' *.png
naol@admin:~/test42$ ls
abc.png document3png document42.png
naol@admin:~/test42$
Page 17
working with files
8. Create a directory called ~/testbackup and copy all files from ~/touched into it.
9. Use one command to remove the directory ~/testbackup and all files into it.
10. Create a directory ~/etcbackup and copy all *.conf files from /etc into it. Did you include
all subdirectories of /etc ?
11. Use rename to rename all *.conf files to *.backup . (if you have more than one distro
available, try it on all!)
Page 18
working with files
8. Create a directory called ~/testbackup and copy all files from ~/touched into it.
mkdir ~/testbackup ; cp -r ~/touched ~/testbackup/
9. Use one command to remove the directory ~/testbackup and all files into it.
rm -rf ~/testbackup
10. Create a directory ~/etcbackup and copy all *.conf files from /etc into it. Did you include
all subdirectories of /etc ?
Page 19
working with files
cp -r /etc/*.conf ~/etcbackup
11. Use rename to rename all *.conf files to *.backup . (if you have more than one distro
available, try it on all!)
On RHEL: touch 1.conf 2.conf ; rename conf backup *.conf
Page 20
Lab manual. working with file contents
In this chapter we will look at the contents of text files with head, tail, cat, tac, more, less
and strings.
We will also get a glimpse of the possibilities of tools like cat on the command line.
Page 21
working with file contents
head
You can use head to display the first ten lines of a file.
naol@admin~$ head /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
root@admin~#
The head command can also display the first n lines of a file.
naol@admin~$ head -4 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
naol@admin~$
tail
Similar to head, the tail command will display the last ten lines of a file.
naol@admin~$ tail /etc/services
vboxd 20012/udp
binkp 24554/tcp # binkp fidonet protocol
asp 27374/tcp # Address Search Protocol
asp 27374/udp
csync2 30865/tcp # cluster synchronization tool
dircproxy 57000/tcp # Detachable IRC Proxy
tfido 60177/tcp # fidonet EMSI over telnet
fido 60179/tcp # fidonet EMSI over TCP
# Local services
naol@admin~$
You can give tail the number of lines you want to see.
naol@admin~$ tail -3 /etc/services
fido 60179/tcp # fidonet EMSI over TCP
# Local services
naol@admin~$
The tail command has other useful options, some of which we will use during this course.
Page 22
working with file contents
cat
The cat command is one of the most universal tools, yet all it does is copy standard input to
standard output. In combination with the shell this can be very powerful and diverse. Some
examples will give a glimpse into the possibilities. The first example is simple, you can use
cat to display a file on the screen. If the file is longer than the screen, it will scroll to the end.
naol@admin:~$ cat /etc/resolv.conf
domain linux-training.be
search linux-training.be
nameserver 192.168.1.42
concatenate
cat is short for concatenate. One of the basic uses of cat is to concatenate files into a bigger
(or complete) file.
naol@admin:~$ echo one >part1
naol@admin:~$ echo two >part2
naol@admin:~$ echo three >part3
naol@admin:~$ cat part1
one
naol@admin:~$ cat part2
two
naol@admin:~$ cat part3
three
naol@admin:~$ cat part1 part2 part3
one
two
three
naol@admin:~$ cat part1 part2 part3 >all
naol@admin:~$ cat all
one
two
three
naol@admin:~$
create files
You can use cat to create flat text files. Type the cat > winter.txt command as shown in the
screenshot below. Then type one or more lines, finishing each line with the enter key. After
the last line, type and hold the Control (Ctrl) key and press d.
naol@admin:~$ cat > winter.txt
It is very cold today!
naol@admin:~$ cat winter.txt
It is very cold today!
naol@admin:~$
The Ctrl d key combination will send an EOF (End of File) to the running process ending
the cat command.
Page 23
working with file contents
copy files
In the third example you will see that cat can be used to copy files. We will explain in detail
what happens here in the bash shell chapter.
naol@admin:~$ cat winter.txt
It is very cold today!
naol@admin:~$ cat winter.txt > cold.txt
naol@admin:~$ cat cold.txt
It is very cold today!
naol@admin:~$
tac
Just one example will show you the purpose of tac (cat backwards).
naol@admin:~$ cat count
one
two
three
four
naol@admin:~$ tac count
four
three
two
one
Page 24
working with file contents
strings
With the strings command you can display readable ascii strings found in (binary) files.
This example locates the ls binary then displays readable strings in the binary file (output
is truncated).
naol@laika:~$ which ls
/bin/ls
naol@laika:~$ strings /bin/ls
/lib/ld-linux.so.2
librt.so.1
__gmon_start__
_Jv_RegisterClasses
clock_gettime
libacl.so.1
...
Page 25
working with file contents
Use cat to create a file named count.txt that looks like this:
One
Two
Three
Four
Five
6. Display catcnt.txt, but with all lines in reverse order (the last line first).
10. Open two terminal windows (or tabs) and make sure you are in the same directory in
both. Type echo this is the first line > tailing.txt in the first terminal, then issue tail -f
tailing.txt in the second terminal. Now go back to the first terminal and type echo This is
another line >> tailing.txt (note the double >>), verify that the tail -f in the second terminal
shows both lines. Stop the tail -f with Ctrl-C.
11. Use cat to create a file named tailing.txt that contains the contents of tailing.txt followed
by the contents of /etc/passwd.
12. Use cat to create a file named tailing.txt that contains the contents of tailing.txt preceded
by the contents of /etc/passwd.
Page 26
working with file contents
Use cat to create a file named count.txt that looks like this:
cat > count.txt
One
Two
Three
Four
Five (followed by Ctrl-d)
6. Display catcnt.txt, but with all lines in reverse order (the last line first).
tac catcnt.txt
10. Open two terminal windows (or tabs) and make sure you are in the same directory in
both. Type echo this is the first line > tailing.txt in the first terminal, then issue tail -f
tailing.txt in the second terminal. Now go back to the first terminal and type echo This is
another line >> tailing.txt (note the double >>), verify that the tail -f in the second terminal
shows both lines. Stop the tail -f with Ctrl-C.
11. Use cat to create a file named tailing.txt that contains the contents of tailing.txt followed
by the contents of /etc/passwd.
cat /etc/passwd >> tailing.txt
12. Use cat to create a file named tailing.txt that contains the contents of tailing.txt preceded
by the contents of /etc/passwd.
mv tailing.txt tmp.txt ; cat /etc/passwd tmp.txt > tailing.txt
Page 27