Useful Linux Commands

Here's a list of commands that we find very useful.  

1. pwd command

Although pwd might seem like a command for a password, it's actually a command to get the current working directory or folder that you are currently located.  The command will return the full absolute path, which starts from the root of the drive. For example, when I'm working on my laptop, my terminal may display a relative path to my home directly which would look like ~/working/projects/geekcafe but if I enter in pwd, it will come back with the full path of /Users/eric.wilson/working/projects/geekcafe

2. man command

This command gives you the manual on any command, program, utility, or function that might be available.  If you're not sure how to use a command or if you want to know all the available options, you can issue the man followed by the command or program

Depending on how many options or how in-depth the manual is, you may need to scroll.  You can control the scroll with your arrow keys going up and down. To exit/quit the manual you press the q key.

Try using the man command on the all the commands on this page

# display the manual for pwd

man pwd

3. cd command

To navigate from one directory to another you use the cd (change directory) command. You can use the full path, Tilda ~ if you are navigating to something in your home path, a path relative based on your current location, and you can even mix it up a little by navigating up some directories during the operation by using two dots (..).  The shell is case sensitive so be sure to type the name in exactly as it is.

Here are some examples:

# navitate to the home folder
cd

## navigate up one directory
cd ..

# navigate up two directories


cd ../..

# navigate to your previous folder (use a hyphen)
cd -

# naviagte to your home folder (use a tilda)
cd ~

# both of these navigate to my Documents directory
cd /Users/eric.wilson/Documents
cd ~/Documents

4. ls command

Use the ls (list) command to list files and folders in your directory. Entering the command by itself will list the contents of your current working directory.

If you want to see the contents of another directory you can change to that directory first using the cd path_to_directory then the ls command or better yet you can simply issue the ls path_to_directory which will display the contents without moving to the directory.

Here are some use options:

  • ls -a shower hidden files
  • ls -l list files in a long format
  • ls -la (combine the two options above)
  • ls -r lower case 'r' shows reverse order
  • ls -s shows the number of file system blocks used by the files
  • ls -R upper case 'R' will recursively traverse the directory.
  • ls -S upper case 'S' will sort by size, used with -r it will reverse list by size 

The ls command supports several optional arguments to control what you see and how you see it.  View the man pages for the ls to see all the available options.

5. cat command

Cat which is short for concatenation reads a file sequentially, and writes the standard output, which is usually the terminal, but you can also redirect that to another file.  So cat can read files and write to files.

# reading file and display it's output to the screen
cat some_file.txt

# create a new file
cat > new_file.txt

# join / combine the contents of files: file_one & file_two to a new file: file_three.txt
cat file_one.txt file_two.txt > file_three.txt

6. pipe "|" command

The pipe is issued with the actual pipe system "|" (without the quotes), is used to push the output of one command into the input of another command.

7. history command

The history command displays the contents of your shell's history file, which tracks all the commands you've entered under that shell.  This is extremely useful if you want to find a command you've entered before but you couldn't remember the name or the full syntax.  When you execute the history command, you'll see a line number and the command. You can then copy and paste that command to your terminal and hit enter to re-execute it.  There's also a better way (see the next command - The "!" command)

8. The "!" exclamation/bang command

This command will help you execute a command that is in your history.  When you view your history (see history command above), the history will also output a number next to it.  If you enter the bang along with the number and hit enter, the entire command will be outputted to the terminal and all you have to do is hit enter to execute it.  

It's nice that it doesn't just execute the line, since you may want to modify it slightly before executing.

9. df command (for disk space information)

The df command is an abbreviation for "disk free", which is used to display the amount of available disk space. It's scoped to only display information based on what the user has access to (min of read privileges). By default (without and additional arguments/options) it displays the size in bytes and the -h option and you get it in a more readable format like M, G, T, P, etc. for Megabytes, Gigabytes, Terabytes and Petabytes.  Depending on the operating system you may see like Mg, Gi, Tb, Pe.

 

# examples:
df

# more readable format
df -h

# include all mount points (including ones with MNT_IGNORE flag)
df -ha

10. top (for memory information)

The command provides real-time information on your current memory usage.  It will keep updating as the information changes, much like the GUI tools you're used to using.  It's very nice to use if you are trying to troubleshoot what process is eating up your memory.

While it is running you can issue several different commands:

  • q - To quit top
  • k - To kill a process, after k you will be prompted for the process id
  • h - This will list all the available options while it is running. 

# examples:

top