Lesson 1

Date: 5/27/2015
User accounts, files and directories, processes, system commands.
Linux System Administration

Systems commands

Directory Manipulation

When you login into the system, you automatically get into your home directory
1. Determine the directory you are in:
2. Go to some other directory, for example, /etc:
      cd /etc 
3. Go to your home directory:
      cd $HOME
      cd ~
4. Go back-and-forth between directories and check where you are:
      cd /etc
      cd -
      cd -
5. Create a new directory above your home directory:
      mkdir Newdir 
6. Go to the new directory and check where you are:
      cd Newdir
7. Go one step back to the parent directory and check where you are:
      cd ..
8. Remove the new directory:
      rmdir Newdir 
9. Create a new directory above your home directory with subdirectories. Try the following:
      mkdir Newdir/one/two/three 
Does it work? Try the following:
      mkdir -p Newdir/one/two/three 
10. Create a few files in Newdir/one:
      cd Newdir/one; touch f1.t; touch f2.t; touch f3.t 
11. Go back into the original directory and try to delete Newdir:
      cd -
      rmdir Newdir 
Does it work ? Try the following:
      rm -R Newdir 
12. Repeat step #10; Copy the directory with its tree into another directory:
      cp -dpR Newdir Newdir-1 
same as
      cp -a Newdir Newdir-1 

13. Show content of file /etc/hosts
cat /etc/hosts 
tac /etc/hosts 
less /etc/hosts
14. Show the first and last 10 lines of a file
head -n 10 /etc/nsswitch.conf   
tail -n 10 /etc/nsswitch.conf     
15. Show date on the system clock
16. File system usage
df -h
17. Disk usage
du -h /home             
du -s /home             
du --max-depth=1 /var    
du -x /                  
du -x /home               
du -x /usr           
18. Display data
echo $[10*3+2]             
echo '$[10*3+2]'          
echo "$[10*3+2]"    
19. Determine file type
file /bin/ls  
file /etc/hosts 
20. Who is logined to the system
21. The owner of the current shell
22. Getting information about a file or command, for example netstat
whatis netstat
apropos netstat 
man netstat
info netstat
  • Recommendation on using man pages to lookup the information about commands: read the NAME, SYNOPSYS, and the first two sentences of the DESCRIPTION, then slide down to the EXAMPLES section. Try, for example:
    man find
           find - search for files in a directory hierarchy
           find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]
           This  manual page documents the GNU version of find.  GNU find searches
           the directory tree rooted at each given file  name  by  evaluating  the
           given  expression  from left to right, according to the rules of 
           precedence (see section OPERATORS), until the outcome  is  known  (the 
           left hand  side  is  false  for and operations, true for or), at which point
           find moves on to the next file name.
           find /tmp -name core -type f -print | xargs /bin/rm -f
           Find  files  named core in or below the directory /tmp and delete them.
           Note that this will work incorrectly if there are  any  filenames
           containing newlines, single or double quotes, or spaces.
    Check out the examples and, if you don't find exactly what you need, look up the options in the OPTIONS section.
    23. Compressing/uncompressing files with gzip, and bzip2:
    cd ~                             
    cp /etc/hosts  hosts.txt      
    gzip hosts.txt                
    ls -l                          
    less hosts.txt.gz             
    more hosts.txt.gz             
    zcat hosts.txt.gz              
    gzip -d hosts.txt.gz           
    bzip2 hosts.txt
    bzip2 -d hosts.txt.bz2
    24. Searching for files by using command find
    cd /tmp                            
    mkdir newfiles; cd newfiles        
    touch fff.txt                      
    cd ~                                
    find / -name fff.txt                
    cd /tmp                             
    find . -name fff.txt                 
    find . -name '*ff*'                   
    find /tmp -name fff.txt -exec rm '{}' ';'    
    find /var -size +1000k                     
    find /var -size +1000000c                   
    find /home -user hostadm              
    find /var -mtime -5                           
    find /var -maxdepth 2 -mtime -20    
    25. Searching for system files
    which dpkg 
    whereis dpkg 
    locate dpkg

    26. Using tar-gzip to archive a directory.
    Create a new directory with subdirectories above your home directory, then tar and gzip it. For example,
    mkdir -p Newdir/one/two/three          
    cd Newdir/one/two                      
    cp /etc/hosts .                         
    cd three                                 
    cp /etc/passwd .                         
    cd ~
    tar -cvf Newdir.tar Newdir
    gzip Newdir.tar
    ls -l Newdir.tar.gz
    rm -R Newdir
    cp Newdir.tar.gz /tmp
    cd /tmp
    gzip -d Newdir.tar.gz
    tar -xvf Newdir.tar 
    27. To tar and gzip file with one command, try the following:
    tar -zcvf Newdir.tgz Newdir         
    rm -R Newdir                         
    ls -l Newdir.tgz                     
    tar -zxvf Newdir.tgz       
    28. Archive/restore from input file list by cpio command:
    cd /etc
    find . | cpio -ov  > /tmp/etc.cpio
    mkdir /tmp/RESTORE
    cd /tmp/RESTORE
    cpio -iv < ../etc.cpio

    29. Command history in bash shell:
    Find a command in the history by the regular expression in its name:
    ctrl + R
    then type the string contained in the command.
    30. Command history file .bash_history is updated when a user exits the shell.
    tail .bash_history
    The size or file .bash_history is defined by env variable HISTSIZE
    echo $HISTSIZE

  • Reference: Basic System Commands

  • Take me to the Course Website