Linux basic commands

  • ls: (-a: all, -l: list, -F: classify, -t: time, -r: reverse, -h: human readable, -S: sort, -i: inode, -d: directory, -n: uid/gid, -R: sub-directories)
    • ls -al; ll            (List all files and directories)
    • ls -F dir            (exe; / dir; symbolic link; > door; = socket; | pipe)
    • ls -d /tmp         (List dir themself not its contents)
    • ls -R                  (Lists sub-directories)
    • ls -lhrt dir        (Sort by by modification time in reverse)
    • ls -lhrS dir       (Sort by size in reverse)
    • ls -d .*              (Show only hidden directories and files)
    • ls- Z file|dir     (List selinux security context)
    • ls -i                    (List inode)
    • ls -n                  (List uid and gid)
  • touch  file
  • mkdir dir
    • mkdir -p /tmp/dir1/dir2
    • mkdir -m 730 /tmp/dir
  • cp file1 file2                                                 (Make a copy of file1 with name as file2)
    • cp -a srcfile destfile                          (Preserve all attributes while copying)
    • cp -r dir1  /tmp                                  (Copy dir into /tmp directory)
  • scp   (copy files from/to remote hosts over network)
    • scp file user@host:/tmp                   (Copy file onto remote host)
    • scp -r dir root@host:/tmp               (Copy dir onto remote host)
    • scp root@host:/tmp/file /tmp/        (Copy file from host to localhost)
    • scp -r root@host:/tmp/dir/ /tmp/   (Copy dir from host to localhost)
  • mv     (Rename or move a file | dir)
    • mv file1 file2                     (Rename file1 to file2)
    • mv dir1 dir2                      (Rename dir1 into dir2)
    • mv file|dir   /tmp              (Move file or dir into /tmp)
  • rm       (Remove files or directories)
    • rm file
    • rm -rf dir             (Remove directory and all of sub-directories and files)
    • rm -rf  dir/*        (Remove all child directories and files)
    • rm  dir/*.txt        (Remove only text files)
  • rmdir dir    (Remove an empty directory) 
  • grep:
    • cat /etc/passwd | grep user
    • grep user /etc/passwd
    • grep -e user1 -e user2   /etc/passwd    /etc/shadow
    • Search a pattern recursivly in files in a directory:
      grep -Eri “error|warn” /var/log/*
      grep -riopen for e-business” /logs/
      grep -ri -C 2open for e-business” /logs/SystemOut*
    • Search for all errors or exception in /var/log directory:
      grep -ri  -E   “error|exception”       /var/log/
      grep -ri  -E   “pattern1.*pattern2”   /var/log/
    • Search a pattern and sort output:
      grepopen for e-business”   $(ls -tr *)
    • Count number of requests in a mon, from a specific IP in Apache logs:
      grep “ /log/ssl_access_log* | grep ‘*/Apr/2015’ | wc -l
    • Exclude one or more string:
      tail -fn1000 ssl_access_logs|grep -vE ‘200|404
    • List regular files, directories and symbolic links:
      ls -al /tmp | grep ^-                        ( Regular files)
      ls  -al /tmp | grep ^d                      (d Directories)
      ls -al /tmp | grep ^[-l]                    (Regular files (d) and symbolic links (l) )
      ls -al /tmp | grep ^– | wc -l           (Count Regular files)
    • Exclude commented and blank lines:
      cat /etc/netconfig | grep -Ev ^#|^$”       
      cat /etc/netconfig | grep ^#                    (Show Commented lines only)
  • find: (-type=f|d-iname=ignore case, -perm=permission, -size, -empty, -exec,
    -mtime|mmin=modified days/mins, -atime|amin=access days|mins, -ctime|cmin) 

    • Search all files recursively:
      find /dir
    • Search all text files:
      find /dir -type f -iname “*.txt”
    • Search files in a directory:
      find /dir -type d -iname dir
    • Search executable, writeable or readable files:
      find /dir -executable|-writable|-readable
    • Search non-executable files:
      find /dir ! -perm -a=x
    • Search SUID (4000) or SGID(2000) files:
      find /dir -perm /u=s|g=s
    • Search sticky bit (1000) files:
      find /dir -perm 1000 -ls
    • Search hidden files:
      find /dir -type f -name “.*” -ls
    • Search files by a user or group:
      find /dir -user user | –group grp
    • Search files by minutes, changed in less than 60 minutes
      find /dir -cmin -60  -ls  (-mmin: Modified mins, -amin: Change mins)
    • Search files by days, modified between 5 and 10 days:
      find /dir -mtime +5 –mtime -10  -ls    (-ctime: Change time; atime: Access time)
    • Search files by Size, greater than 5 and less than 10 MB
      find /dir -size +5M -size -10M  -ls
    • Search files and change their permissions:
      find /dir -type f -perm  777 -exec chmod 644 {} \;
    • Search and remove files:
      find /dir -maxdepth 1 -type f -empty -exec rm -rf {} \;
    • Search and move files:
      find /dir -type f -iname “*.log” -size +10M -exec mv {} /dir2  \;
    • Search and copy files:
      find /dir -type f -iname “*.log” -size +10M -exec cp {} /dir2  \;
    • Search and remove files:
      find /dir -type f -iname “*.log” -size +10M -exec rm -f {} \;
    • Search files and calculate their total size:
      find /dir -type f size +10M -exec du -csh {} +   (Calculate total size)
    • Seach and compress files in a directory:
      find /dir -maxdepth 1  -type f  -name “*.log” | tar -zcvf  log.tgz -T –
      tar  -zcvf  backup.tgz  $(find /dir -type f  -iname “*.log”)
    • Find unowned files and directories:
      find / -nouser -nogroup 2> /dev/null
    • Exclude one or more directories in search:
      • find / -path /proc -prune -o -size +50M -print
      • find . -path ./misc -prune -o -name ‘*.txt’ -print
      • find . -type d \( -path dir1 -o -path dir2 -o -path dir3 \) -prune -o -print
  •  sed:
    • sed   s/str1/str2/    file
    • sed   s/str1/str2/    file > file2          (Save updates in file2)
    • sed -i   “s/and/\&/g”   file                 (-i: Save; special chars must be escaped by \)
    • sed -i.bak  “s/first/last/gI” file             (-I: Ignore case; -i.bak backup file)
    • sed  “s/\s\+/ /g” file                                  (Replace all white-spaces to a single space)
    • sed -n /^#/p   /etc/netconfig                   (Show only commented lines from the file)
    • sed ‘/^#\|^$/d’ /etc/logrotate.conf        (Exclude commented and blank lines)
    • sed -n /11\/Nov/,/12\/Nov/p’ /file.log   (Prints all lines between 11/Nov to 12/Nov)
    • sed   1,3s/str1/str2/g   file                     (Replace all occurrences from lines 1 to 3)
    • sed -e  ‘s/01/JAN/’   -e    ‘s/02/FEB/’   -e   ‘s/03/MAR/’ file.txt
    • ls /home | nl | sed   “s/^ *//g”    (Remove starting/leading white spaces)
    • ls /home | sed   s/\t/\r/g   file       (Replace all tabs with new line)
  • shutdown -r|-h now; reboot; poweroff
  • telinit|init 0|6|1|3|5   (0=shutdown, 6=restart, 1=single-user, 3=cmd, 5=graphical)
  • runlevel; who -r                                  (Shows current run level)
  • time curl               (calculate time of a command)
  • curl -o mypage.html
  • curl -X POST -T file.xml -H “Content-Type: application/xml” http://localhost:8181/cxf/crm/customerservice/customers  (send POST request)
  • wget  > ~/test.html (Download mypage.html)
  • uname -a | -r  (Prints system information)
  • watch  date   (Runs a command repeatedly)
  • ./configure; make; make install    (Install from source)
  • gcc -o file  file.c; make   file.c               (Compile a C file)
  • chroot /mnt/sysroot
  • stat -f /dev/sda                     (Show file or files system status)
  • echo 9*8|bc                         (Calculator)
  • updatedb                             (Update db for locate)
  • locate file  [i|-n 5]                                   (Find files by name) 
  • whoami                                                     (Shows effective or currently login userid)
  • who -a; users; w                                      (List currently logged in users)
  • last; lastlog                                              (Show listing of last login users)
  • date -d “+10 days”                                    (Calculate future date after 10 days)
  • virsh  list                                                   (List only running virtual machine in KVM)
  • virsh list –all|version|nodememstatus| nodecpustatus  (KVM hypervisor)
  • ldd /bin/bash$(which top)                 (Shows shared library dependencies)
  • udev-> /etc/udev/udev.conf; /etc/udev/rules.d/*.rules; /usr/lib/udev/rules.d/*.rules

List and manipulate file contents:

  • less file                                 (Show contents of the files)  
  • more file                           
  • cat file                                  (Show contents of the file from top)
    • cat <<EOF   (end marker)
      echo “Hello”
  • tac file                                 (Show contents of the file from bottom)
  • tail -fn 1000 file                 (Keep on showing last 1000 lines of the file)
  • dmesg    -w                        (show kernel messages)
  • head file                              (Show by default top 10 lines of the file)
  • diff [-yfile1  file2               (Show difference between two files)
  • diff -r dir1 dir2                   (Show difference between two directories)
  • lsof  [/mnt|/dev/sda]          (Show open files)
  • ln -s /bin/bash /tmp/bash   (Create soft symbolic link)
  • ln /bin/bash /tmp/bash      (Create hard symbolic link)
  • file   file                                 (Determine file type)
  • stat  file | -f /dev/sda1         (shows file or file system status) 
  • which bash                           (Shows the full path of command)
  • cut -d : -f1,6 /etc/passwd     (-d: delimiter, -f: fields)
  • awk -F : ‘{print $1,$6}’ /etc/passwd   (-F: delimiter, print $1 $6: fields)
  • ip addr show dev ens33 | grep -w “inet” | awk ‘NR==1{print $2}’ | cut -d’/’ -f 1
  • df -hT | awk   ‘/tmpfs/   {print $1,”\t”,$7}’
  • ls /home | sort    (-r: reverse, -n: numeric, -f: ignore case, -b: ignore blanks)
  • ls /home | tr [:lower:] [:upper] | ‘a-z’ ‘A-Z’       (Converts lower to upper case)
  • cat file | tr -s ‘ ‘                                   (Remove extra white spaces)
  • echo “Hello” | cut -c 1,2,3,4              (Prints chars 1,2,3,4)
  • nl  /etc/passwd; ls /home | nl           (Add line numbers)
  • uniq -cd |-cu file                                (-c: count; -d: duplicate, -u: unique) 
  • join file1 file2                                      (Join lines of two files on a common field)
  • wc -l|-w|-c  file                                   (-l: Line count, -w: Word count, -c: Char count) 
  • echo “1st line” | tee -a /tmp/line.txt   (-a append contents to the file)
  • mktemp /tmp/file.XXXX                        (Make a temp file)
  • mktemp -d /tmp/dir.XXXX                    (Make a temp directory)
  • last | grep user | tr -s ‘ ‘ | cut -d’ ‘ -f1,3 | sort -k2 | uniq
  • md5sum file
  • File globing:
    • echo {Sun,Mon,Tues,Wed,Thurs,Fri,Sat}.log
    • touch file{1..5}.log             (Creates file1.log, file2.log and file3.log..)
    • touch file_{a,b}{1,2}.log   (Creates file_a1.log, file_a2.log, file_b1.log..)
  • Redirect stdout (1) and stderr (2):
    • du -csh /*  >  result 2>&1         (Sends stdout and stderr to out.log)
    • du -csh /*  &> result                 (Abbreviation of above, for Append use “>>” )
    • du -csh /*  > result  2> error   (Sends stdout in result and stderr in error)   


  • echo “user ALL=(ALL) NOPASSWD: ALL” | sudo tee /etc/sudoers.d/user
  • gpasswd -a user wheel    (Add a user into wheel group /etc/sudoers file)
  • echo “%grp ALL=(ALL) NOPASSWD: ALL” | sudo tee /etc/sudoers.d/grp
  • echo  “user ALL=NOPASSWD: /usr/bin/ls, /usr/bin/cat” | tee /etc/sudoers.d/user
  • sudo ls -l /etc/
  • sudo su user|sudo -iu user     (Sudo to user which don’t have password)
  • sudo -i  | sudo su –                    (Sudo to root user)
  • Configs:
    • /etc/sudoers.d/*
    • /etc/sudoers
      • %wheel ALL=(ALL) ALL       (Default will prompt for password)
      • %wheel ALL=(ALL) NOPASSWD: ALL


  • chsh -l; cat /etc/shells           (List installed shells)
  • echo $SHELL                         (Prints default  shell)
  • echo $0; ps -p “$$”                 (Prints current shell)
  • chsh -s /bin/tcsh user;          (Change user shell in /etc/passwd)
  • exec bash                               (Restart shell without logging out)
  • chroot /mnt/sysroot             (Changes the apparent root directory)
  • bash autocomplete:             (Enable bash command auto complete)
    • yum install bash-completion
    • source /etc/profile.d/
  • Shell terminal shortcut keys:
    • ctr+l : Clear the screen
    • ctr+c|ctr+z : Terminate (c) and suspend or stop (z) the command
    • ctr+d : close the bash shell
    • ctr+a|ctr+e : Move cursor to the beginning (a) and end (e) of screen
    • ctr+k|ctr+u : Cut all chars from cursor to end (k) and to beginning (u)
    • ctr+y: Paste the chars copied by ctr+k or ctr+u
    • ctr+r | ctr+g: Search a command from history (r), exit history (g)
    • ctr+s|ctr+q : Stop (s) and resume (q) output on the screen
    • ctr+w : Delete a word before cursor  (Never use in Exam, closes browser tab)
    • alt+d : Delete a word after cursor
    • alt+b : Move one word back
    • alt+f : Move one word forward
    • ESC . : Append options from the last command

Environment variables:

  • Set user local environment variable:
    • Setting temporary user variables:
      • VAR=”value”; echo $VAR                   (Visible in current shell only)
      • export VAR=”value”; echo $VAR     (Visible in current and child shell)
    • Setting permanent user variables:
      • echo ‘export JAVA_HOME=”/usr/lib/jvm”‘ >> .bash_profile
      • source .bash_profile
  • Set system-wide environment variable:
    • echo ‘VAR2=”value”‘ >> /etc/environment
    • P.S. You need to logout and login to reload the file
  • Set application specific environment variable for all users:
    • echo “export JAVA_HOME=/usr/lib/jvm” > /etc/profile.d/
    • echo “export CLASSPATH=.” > /etc/profile.d/
    • chmod +x /etc/profile.d/
  • export; env                                                            (Show all environment variables)

alias and unalias:

  • alias calcfs=’sudo du -xhd 1 /’                (Add an alias)
  • calcfs                                                           (Test alias calcfs) 
  • unalias chkfs                                            (Remove usercmd alias)
  • echo “alias calcfs=’sudo du -xhd 1 /’ >> .bashrc    (Persistence)
  • source .bashrc

ssh: (Secure shell)

  • yum install openssh-server openssh-clients
  • ssh user@host | ssh -l user host | ssh -i  ~/.ssh/id_rsa   user@host
  • ssh user@remotehost  “ls /home”      (Execute command at remote host)
  • ssh-keygen  [-qf ~/.ssh/id_rsa  -N ‘password’ -t rsa]    (Generate ssh key in ~.ssh/)
  • ssh-copy-id  user@host2                      (Copy user public key onto host)
  • ssh -X  user@host                                  (Enable X11 forwarding)
  • ssh -A  user@host      (Enables forwarding of the authentication agent conn)
    • ssh-add ~/.ssh/id_rsa         (Use ssh agent to cache private key)
    • eval $(ssh-agent)               (Start ssh agent)
  • systemctl start|stop|restart|status sshd
  • Config files:
    • /etc/ssh/sshd_config  (server config file)
      • PermitRootLogin  no|without-password  (Disable root password auth)
      • PasswordAuthentication  no  (Disable password auth for all remotely)
      • X11Forwarding                 yes|no
      • AllowAgentForwarding   yes|no
    • /etc/ssh/ssh_config
    • $HOME/.ssh/*                  (ssh directory own by user and 700 permissions)
      • id_rsa                       (private key, owned by user and 600 permission)
      • authorized_keys     (public keys list, owned by user and 600 permission)
      •                (public key, owned by user and 644 permission)
      • known_hosts           (Hosts list to allow login)
      • config > /etc/ssh/ssh_config      (Client config file)
        • ForwardX11Trusted yes  (Tunnel X11 protocol over ssh)

grub2:  (Boot loader)

  • grub2-mkconfig  -o /boot/grub2/grub.cfg   
  • grub2-*
  • cat /boot/config-$(uname -r)             (Show all Kernel boot loader parameters) 
  • Configs:
    • /boot/grub2/grub.cfg              (Main config file)
    • /etc/default/grub                      (Default boot loader parameter)
    • /etc/grub.d/*

Log files:

  • syslog | rsyslog:  (/etc/rsyslog.conf; /etc/rsyslog.d/)
    • echo “authpriv.alert”  /var/log/auth.log” | tee /etc/rsyslog.d/auth.conf
    • systemctl restart rsyslog
    • logger -p authpriv.alert  “Test authpriv.alert message”
    • tail /var/log/auth.log
  • /var/log/messages  (Global system messages)
  • /var/log/dmesg        (Kernel ring buffer messages)
  • /var/log/secure        (Authentication and authorization messages)
  • /var/log/cron            (Cron jobs messages)
  • /var/log/boot.log     (System boot messages)
  • /var/log/yum.log
  • /var/log/firewalld
  • /var/log/httpd/access_log; /var/log/httpd/error_log
  • /var/log/httpd/ssl_access_log; /var/log/httpd/ssl_error_log 
  • /var/log/audit/audit.log

/proc file system:

  • cat /proc/meminfo – Info about system memory
  • cat /proc/swap – Information about swap space.
  • cat /proc/mounts – List of all mounts in use by system.
  • cat /proc/partitions – Detailed info about partitions available to the system.
  • cat /proc/filesystems – Current filesystems supported by the kernel
  • cat /proc/cmdline – Kernel command line information.
  • cat /proc/uptime – Uptime information (in seconds).
  • cat /proc/version – Kernel version, gcc version, and Linux distribution installed.
  • cat /proc/modules – Currently loaded kernel modules.
  • cat /proc/console – Information about current consoles including tty.
  • cat /proc/devices – Device drivers currently configured for the running kernel..
  • cat /proc/dma – Info about current DMA channels.
  • cat /proc/fb – Framebuffer devices.
  • cat /proc/iomem – Current system memory map for devices.
  • cat /proc/ioports – Registered port regions for input/output communication
  • cat /proc/loadavg – System load average.
  • cat /proc/locks – Files currently locked by kernel.
  • cat /proc/misc – Miscellaneous drivers registered for miscellaneous major device.
  • cat /proc/pci – Information about every PCI device.
  • cat /proc/stat – Record or various statistics kept from last reboot.

About Ishtiaque

I am IBM Certified Infrastructure Systems Architect, Linux Foundation Certified System Administrator, Oracle Certified Programmer in Java and Web Component Developer, and TOGAF 9 certified with over 10 years of support and development experience in IBM middleware software and Java. Additionally, have a sound grip in databases and OpenStack administration. I hold the following certifications: IBM Certified Infrastructure Systems Architect Linux Foundation Certified System Administrator (LFCS) TOGAF 9 Certified Oracle Certified Expert, Java EE6 Web Component Developer Oracle Certified Professional – Java 6 Programmer ITIL v3 Foundation Certified IBM Certified Solution Architect – Cloud Computing Infrastructure V1 IBM Certified System Administrator – WebSphere Portal V8, V7, V6.1, V6 IBM Certified System Administrator – WebSphere Application Server V7, V6.1 IBM Certified System Administrator – AIX V7 IBM Certified System Administrator – WebSphere MQ V7 IBM Certified Deployment Professional – Business Process Manager Advanced V7.5 IBM Certified Solution Advisor – Cloud Computing Architecture V3 IBM Certified Solution Developer – WebSphere Portal V5.1
This entry was posted in LFCE, LFCS, Linux. Bookmark the permalink.