Linux Commands

Edit me

Commands are directives to a command-line interpreter or a shell. Commands usually accept command-line arguments such as the target path of an operation or options to alter the operation of the command. There are two types of commands: shell builtins and external programs. Shell builtins usually implement simple or trival functions and are executed directly within the shell, whereas external programs are loaded and executed in a separate process.

Basic Commands

  • ls, cp, mv, ps, kill, echo, cat, man, ifconfig

File Management

  • cd dir – Change current directory to dir
  • pwd – Print path of current working directory
  • pushd, popd
  • ls -l – print directory listing of current directory in long format
  • ln -s target link-name – Create a symbolic link named link-name that points to target
  • readlink
  • cp file1 file2 – Copy file1 to file2
  • cp -r src dest – Copy directory src to dest
  • mv file1 file2 – Move or rename file1 to file2
  • rm -f file(s) – Delete file(s)
  • rm -rf dir – Remove directory dir and all of its contents
  • mkdir dir – Create a directory named dir
  • mkdir -p foo/bar/baz – Create a directory and all intermediate directories
  • mknod, mkfifo
  • touch file – Create an empty file named file
  • df -h – Show disk free space
  • du -sh dir – Show cumulative size of a directory
  • stat, file
  • chmod octal file – Change the permissions of file to octal
  • chmod 777 file – make file readable, writable, and executable (rwx) for all users
  • chmod +x file – make file executable
  • chown user path – Change owner of file or directory
  • chown user:group – Change owner and group of file or directory
  • chown -R user dir – Change owner of directory and contents (recursively)
  • chgrp
  • cat file – Display the contents of file
  • more file – Display file one page at a time; press spacebar to proceed to the next page
  • less file – less is similar to more, but also allows moving backwards
  • head -20 file – Display the first 20 lines of file
  • tail -f file – Display the contents of file as it grows – This is especially usefull for following log files.
  • vi, vim, nano, pico, emacs
  • which command – give the full path to command
  • find dir -type f -name 'foo*' – find all files in dir whose name starts with “foo” (note the single quotes, otherwise shell globing would occur)
  • find . -type f -exec grep -l "foo" {} \; – find all files in the current directory containing “foo”
  • locate file – find file
  • diff -u file1 file2 – view the differences between two files
  • diff -ur dir1 dir2 – view the differences between two directories

File System and Partitioning

  • mount, umount
  • lsblk, blockdev, fdisk, fsck, mkfs, resize2fs

Text and Stream Processing

  • strings file – print all human readable strings in a binary file
  • grep pattern file – find all lines in file that contain or match pattern
  • dd
  • pv, tee
  • sed, awk, tr, cut, join, column, iconv
  • wc -l file – count number of line in file
  • nl, od, sort, uniq, fold, tac
  • csplit, split
  • xargs

File Compression

  • tar zcvf archive.tar.gz files... – create a gzip-compressed archive
  • tar zxvf archive.tar.gz – decompress and extract archived files to current directory
  • gzip file.txt – compresses a file (creates file.txt.gz) and removes the original, uncompressed version
  • gunzip file.txt.gz – uncompresses a file that was compressed with gzip or compress and removes the compressed version
  • zip -r archive.zip files... – create a zip archive
  • unzip archive.zip [-d destination_folder] – extract/decompress zip achive
  • unzip -l archive.zip – list the contents of archive.zip
  • zcat, zless, zmore, zgrep, zdiff – these ‘Z’ commands operate on gzip-compressed files and provide a ‘Z’ equivalent to their regular counterparts
    • zcat system.log.0.gz – view a compressed file
      • zcat is identical to gunzip -c
    • zless system.log.0.gz – ‘Z’ equivalent of less to view a compressed file

Getting Help

  • man command – Show the manual for command
  • help command – Use help for builtin commands
    • If you do not know if a command is a shell builtin or not, run type command.
  • info command – for GNU/Linux commands, info often provides a more detailed documentation than the man page
  • apropos subject – Search manual pages for subject
    • man -k is equivalent to the apropos command
  • whatis command – Display one-line manual page descriptions
    • use help -d for a one-line description of a shell builtin
  • which command – Show the full path of a command
  • whereis command – locate the binary, source, and manual page files for a command

Process Management

  • ps aux – list all processes on the system
  • top, pstree
  • kill, pkill, killall
  • disown, nohup
  • lsof, fuser

Managing System Services

  • service, systemctl
  • init, shutdown

User Management

  • usermod, useradd, adduser, userdel, passwd
  • groupmod, groupadd, gpasswd
  • whoami – Show who you are logged in as
  • id, getent, groups
  • who, w, last

Network Commands

  • ifconfig – List IP addresses and configuration of network interfaces
  • ifdown, ifup
  • dhclient -r eth0 – release current DHCP lease
  • dhclient -v eth0 – obtain IP address from DHCP
  • ip, route
  • iptables
  • wpa_supplicant
  • iwlist – display information about wireless networks
  • wpa_cli
  • brctl, hostapd
  • wget, curl
  • ftp
  • scp -P 5010 foo.txt user@adams204xx.hofstra.edu:/home/user/ – copy local file to linux home directory
  • scp -P 5010 user@adams204xx.hofstra.edu:/home/user/foo.txt foo.txt – copy remote file to local system
  • sftp, tftp, rsync
  • ping, nmap, traceroute, arp, tcpdump
  • nslookup domain – Perform DNS lookup of domain
  • dig domain – Perform DNS lookup of domain
  • ssh user@host – Connect to host as user
  • ssh-keygen -t rsa -b 4096 – Generate a new SSH key-pair
  • ssh-copy-id user@host – Copy your local key (usually ~/.ssh/id_rsa.pub by default) to the account for user on host to enable key-based (passwordless) authentication into the remote host

System Information

  • uname -a – Show kernel information
  • dmidecode
  • lscpu, lspci, lsusb
  • hostname
  • date, uptime
  • free – Show memory and swap usage

Printing

  • lp, lpr

Environment

  • env, chroot

Shell builtins

  • cd, pwd
  • clear
  • exit
  • history, fc
  • type