Table of Content

Weight: 4

Description: Candidates should be able to use the basic Linux commands to manage files and directories.

Key Knowledge Areas:

  • Copy, move and remove files and directories individually
  • Copy multiple files and directories recursively
  • Remove files and directories recursively
  • Use simple and advanced wildcard specifications in commands
  • Using find to locate and act on files based on type, size, or time
  • Usage of tar, cpio and dd

Terms and Utilities:

cp
find
mkdir
mv
ls
rm
rmdir
touch
tar
cpio
dd
file
gzip
gunzip
bzip2
xz
file globbing

pwd

pwd command same as $PWD env variable

echo $PWD
ls $PWD

ls

ls -al # long format
d directory
- regular file
l symbolic link
b block device
c char device

ls -i # inode

ls -lS # sort by size bytes
ls -ltr # reserve sort by mtime

cp , mv , rm

cp -b | --backup source targetdir # create backup before copy
cp -p s1 t1 # preserve attributes like owner:group

mkdir -p dir1/dir2/dir3
rm -r dir1 # delete all dir1/dir2/dir3/*

globbing

wildcard

~ /home/user or /root (the user's home directory) echo ~/.bashrc
* Any sequence of characters (or no characters) echo /etc/*ost*
? Any one character echo /etc/hos?
[abc] Any of a, b, or c echo /etc/host[s.]
{abc,def,ghi} Any of abc, def or ghi. echo /etc/hosts.{allow,deny}

touch

touch -t 201511032200.59 file

touch -d 11am file

date -r f1 # save refer file f1
touch -r f1 f1a # create new f1a refer f1 timestamp

mtime modify time
atime access time

find

find . -size 0
find . -size -26c -size +23c -exec ls {} \;     # size >=23 bytes and = 60 mins, 

file

oldhorse@dclab-u1504s:~$ file test2
test2: ASCII text
oldhorse@dclab-u1504s:~$ file -i test2
test2: text/plain; charset=us-ascii

gzip

gzip -N filename   # generate filename.gz and preserved name , timestamp
gzip -d filename.gz

bzip2

bzip2 file
bzip2 file.bz2

bzcat   # same as  bzip2 -dc , view inside bz2 without decompressed 
bzip2recover  #  recover data from damaged zip2 file

tar

tar -zcvf file.tar files
tar -tvf file.tar

xz

tar -cJf backup.xz backup/
tar -tJf backup.xz
tar -xJf backup.xz

cpio

find backup/ -depth -print0 | cpio --null -o > backup.cpio   # cpio using absolute filename
cpio -i --list "*backup*" 

dd

dd if=test1 of=test2  # copy test1 to test2
dd if=test1 conv=ucase 

dd if=/dev/sda2 of=backup-1

sudo dd if=/dev/sda2 of=backup-sda2    # backup partition
sudo dd if=/dev/sda2 |gzip > backup-sda2.gz

gunzip backup-sda2.gz -c | dd of=/dev/sda7    # restore to partition dsa7

Quiz questions

1. What is the parameter for copy, move and remove that requests confirmation from the terminal before acting?
2. Who is the owner of a file created using touch?
3. Which parameter for copy will cause it to copy file permissions and ownership as far as possible?
4. How do you find files which have not been modified for more than 2 days?
5. How do you find executable files which have the set-gid bit set?

Answers to quiz questions

1. -i
2. The user that ran the command.
3. -a
4. find -mtime +2
5. find -type -f perm 2001