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 -lhtr dir        (List by modification time in reverse in human readable)
    • ls -lhS dir         (List in human-readable format and sort by size)
    • ls -d .*              (Show hidden 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 a directory and all of sub-directories and files)
    • rm /dir/*.txt        (Deletes only text files)
    • rm -rf /dir/*        (Deletes all files and directories)
  • 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 -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 “192.68.1.1 /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 –
    • 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          (Special chars must escaped by backslash)
    • sed -i.bak  “s/first/last/gI” file   (Take backup of the file and replace all)
    • 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                                                (Shows current run level)
  • time curl http://google.com               (calculate time of a command)
  • 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                             (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)
  • 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)
  • 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)
  • 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)
  • 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: count lines; -w: count words, -c: count chars) 
  • echo “1st line” | tee -a /tmp/line.txt   (-a append contents to the file)
  • mktemp /tmp/file.XXXXXXXX              (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
  • 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):
    • ls -l > /dev/null  2>&1                              (Redirect stdout and stderr to /dev/null)
    • find /etc/ -name >> /tmp/out.log 2>&1  (Appends stdout and stderr to out.log)
    • fine /etc/ -name &> /tmp/out.log            (Combine stdout and stderr)
    • find /etc/ -name passwd  > /tmp/output  2> /tmp/error

sudo:

  • echo “user ALL=(ALL) NOPASSWD: ALL” | sudo tee /etc/sudoers.d/user
  • gpasswd -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 to user which don’t have password)
  • sudo -i                                        (Sudo to root user)
  • Configs:
    • /etc/sudoers.d/*
    • /etc/sudoers
      • %wheel ALL=(ALL) ALL       (Default will prompt for password)
      • %wheel ALL=(ALL) NOPASSWD: ALL

shell:

  • 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)
  • Shell terminal shortcut keys:
    • ctr+a : Move to the beginning of command
    • ctr+e : Move to the end of command
    • ctr+k : Delete all chars from cursor to the end
    • ctr+u : Delete all chars from cursor to the beginning
    • 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:
    • export VAR=’value’; echo $VAR                           (Set a variable temporarily)
    • echo export VAR1=’value’ >> .bash_profile      (Set a variable permanently)
    • source .bash_profile
  • Set system-wide environment variable:
    • sudo bash -c “echo VAR2=’value’ >> /etc/environment
    • P.S. You need to logout and login to reload the file
  • export; env                                                              (Show all environment variables)

ssh: (Secure shell)

  • yum install openssh-server openssh-clients
  • ssh user@host | ssh -l user host
  • ssh -i   ~/.ssh/id_rsa    user@host
  • ssh -X user@host                                   (Enable X11 forwarding)
  • ssh user@remotehost  “ls /home”      (Execute command at remote host)
  • ssh-keygen  [-f ~/.ssh/id_rsa   -N ‘password’ -t rsa]    (Generate ssh key in ~.ssh/)
  • ssh-copy-id  user@remotethost                           (Copy user public key onto host)
  • eval $(ssh-agent)               (Start ssh agent)
  • ssh-add ~/.ssh/id_rsa         (Use ssh agent to cache private key)
  • 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/*
      • id_rsa                        (private key, should have 600 permission)
      • id_rsa.pub                (public key, should have 644 permission)
      • authorized_keys     (public keys list, MUST have 600 permission)
      • known_hosts           (Hosts list to allow login)
      • config > /etc/ssh/ssh_config      (Client config file)
        • ForwardX11Trusted yes  (Tunnel X11 protocol over ssh)

/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.

Log files:

  • syslog | rsyslog:  (/etc/rsyslog.conf; /etc/rsyslog.d/)
    • echo “*.debug”  /var/log/debug.log”  | tee /etc/rsyslog.d/debug.conf
    • systemctl restart rsyslog
    • logger -p user.debug  “Test debug message”
    • tail /var/log/debug.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

 

Advertisements

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 LFCS, Linux. Bookmark the permalink.