Basic Linux Commands

Linux

Here are some basic Linux commands to get you up and running, or a quick cheat sheet. Great for when working on a Raspberry Pi or any other single board computers which use Linux commands such as Beagleboard / Beaglebone, Asus Tinker Board and many others.

 

FILES & NAVIGATING

  • ls – directory listing (list all files/folders on a current directory)
  • ls -l – directory listing, with the file details in a formatted way, easier to read
  • ls -la – directory listing the same as ls -l but this will also display hidden files
  • cd dir – change directory to dir (dir will be directory name)
  • cd .. – change to the parent directory
  • cd ../dir – change to dir in the parent directory (dir is replaced with a specific directory)
  • cd – change to the home directory
  • pwd – show current directory
  • mkdir dir – create a directory dir (replace dir with your chosen name)
  • rm file – delete a file
  • rm -f dir – force remove a file
  • rm -r dir – delete directory dir
  • rm -rf dir – remove directory dir
  • rm -rf / – if run by a superuser, will delete every file on the system (DO NOT DO THIS)
  • cp file1 file2 – copy file1 to file2
  • mv file1 file2 – rename file1 to file2
  • mv file1 dir/file2 – move file1 to dir as file2
  • touch file – create or update file
  • cat file – output contents of a file
  • cat > file – write standard input into a file
  • cat >> file – append standard input into a file
  • tail -f file – output contents of a file as it grows

NETWORKING

  • ping host – ping host
  • whois domain – get whois for a domain
  • dig domain – get DNS for domain
  • dig -x host – reserve lookup host
  • wget file – download file
  • wget -c file – continue a stopped download
  • wget -r url – recursively download files from a URL
  • curl url – outputs the webpage from a URL
  • curl -o hello.html URL – writes the page to hello.html
  • ssh [email protected] – connect to host as user
  • ssh -p port [email protected] – connect using port
  • ssh -D [email protected] – connect & use bind port

PROCESSES

  • ps – display currently active processes
  • ps aux – detailed outputs
  • kill pid – kill process with process id (pid)
  • killall proc – kill all processes named proc

SYSTEM INFORMATION

  • date – show current date / time
  • uptime – show uptime
  • whoami – currently logged in user
  • w – display who is online
  • cat /proc/cpuinfo – displays CPU information
  • cat /proc/meminfo – displays memory information
  • free – show memory and swap usage
  • du – show directory space usage
  • du -sh – displays readable sizes in GB
  • df – show disk usage
  • uname -a – show all kernel information. Alternatively, if you only want specific information, you can change the “-a” switch to any of these for different information: “-m, -r, -v, -n”
  • sudo lshw – calls the lshw tool which gathers vast information about the computer’s hardware such as CPU, disks, memory, USB controllers etc
  • lscpu – display CPU information
  • lsusb – displays USB controllers information
  • lspci – displays PCI devices information

COMPRESSING

  • tar cf file.tar files – tar files into file.tar
  • tar xf file.tar – untar into a current directory
  • tar tf file.tar – show contents of archive
    • Switches:
      • c – create an archive
      • t – table of contents
      • x – extract
      • z – use zip/gzip
      • f – specify a filename
      • j – bzip2 compression
      • w – ask for confirmation
      • k – do not overwrite
      • T – files from a file
      • v – verbose

PERMISSIONS

  • chmod octal file – change permissions of a file
    • 4 – read (r)
    • 2 – write (w)
    • 1 – execute (x)

order owner/group/world

  • chmod 777 -rwx for everyone
  • chmod 755 – rw for the owner, rx for group world

OTHER USEFUL COMMANDS

  • grep pattern files – search in files for a pattern
  • grep -r pattern dir – search for pattern recursively in dir
  • locate file – find all instances of a file
  • whereis app – show possible locations of app
  • man command – show manual page for a command
Share This
X