How To Step Up Your Command Line Game with Bash

A photograph of a screen showing the output of the 'ls' (list) command on a Unix operating system, with green and blue text on a black background
Photo by Pixabay

Bash, or the Bourne Again Shell, is a popular command-line interface.  Stephen Bourne created the Bourne Shell for the Unix operating system, and Brian Fox released his improved version, Bash, in 1989.  Today, Bash is one of the most widely used shells in Unix and Unix-like operating systems, including MacOS* and Linux.  Bash’s ubiquity and its powerful scripting capabilities have made it a perennial favorite among system admins, software professionals, and *nix power users–so it’s important for new techies to get at least a few Bash commands in their programming toolkit.

Commands for Navigating the File System 

pwd

pwd stands for “print working directory”, and running this command will display the current working directory in the terminal.  It’s useful for letting you know where you are in the file system! 

pwd

ls

The “list” command, ls, prints the contents of the current working directory to the terminal.  When you use the command without any other options, it will exclude hidden files from the command output. Hidden files are often used to store configurations or other information that doesn’t need to be viewed by users, so this default behavior usually isn’t a problem.  If you want to show hidden files, add the -a option flag to the command, ls -a

ls

ls -a

cd

To navigate between directories (folders), use the cd command.  You can use it in several different ways to move between directories: 

cd <folder> #move into a subfolder

cd <folder/subfolder> #move into a nested subfolder

cd .. #moves back up the tree by one directory

Commands for Interacting with Files & Directories

mkdir

The mkdir or ‘make directory’ command does exactly what it’s named for: when given a directory name,  it creates a new directory with that name in the current working directory; when given a file path, it creates a new directory at that file path.  The -p option should be included when creating file paths if you need bash to create any parent directories (containing folders) as well as creating the new subdirectory at the end of the path.  

mkdir new_folder

mkdir -p path/to/new/folder/new_folder

touch

touch creates an empty file in the current working directory.  You can use the command to create multiple empty files at once. 

touch file.txt

touch file.txt file2.txt file3.txt

cp and mv 

The cp command copies a file or directory to another location, while the mv command moves a file or directory to another location. 

cp file.txt file_copy.txt

mv file.txt path/to/new/directory

rm 

Be very careful using this command!  It permanently deletes files and/or directories from the file system–and once you remove them, they’re gone forever.  If you attempt to remove a directory that has other directories and/or files inside of it, the command will fail. To remove a directory and all files contained in it, you’ll need to add the -r option. 

rm file.txt

rm -r path/to/directory

find

This command locates files and directories based on information such as name, size, or modification timestamp.  You’ll need to give the command a location to search, an option, and search criteria.  You can also use multiple options and search criteria. 

find <path/to/directory> -name “file.txt” #find a file named ‘file.txt’ 

find <path/to/directory> -type d #find all directories 

find <path/to/directory> -mTime -7 #find files and directories modified in the past 7 days

find <path/to/directory> -name “file.txt” -type f -mTime -7

Conclusion

Whether you’re a veteran software professional or just starting out, knowing a few Bash commands is a great idea.  Bash’s popularity means it can be found on almost any computer system, and being able to navigate the file system and interact with files and directories no matter what operating system is in use will definitely come in handy.  Being able to navigate a file system with Bash puts you in a good spot to use git version control from a command prompt, and it’s also very useful for interacting with remote computers and virtual machines through ssh!  Also… it makes you look really cool at parties, and impresses people who can only use a graphic user interface.  Happy Bashing! 


*newer versions of MacOS may default to using Z Shell (zsh), a superset of Bash. You can use the same commands shown here to navigate with zsh!

Add a Comment