Let’s face it, if you’re a developer, especially a back-end web developer, you have got to know some Bash commands. You’ll use them daily to run your server-side scripts, start up your database, and even perform some basic housekeeping tasks. So in this guide, we’ll look at 11 basic Bash commands every developer should know, with explanations and examples.
Contents
- pwd: Print Working Directory
- ls: List
- cd: Change Directory
- touch
- rm: Remove
- mkdir: Make Directory
- rmdir: Remove Directory
- mv: Move
- cp: Copy
- alias
- man
1. pwd
: Print Working
Directory
The pwd
command is like asking the computer a question: Where the hell am I?
The response is the full path to the directory you are currently logged to, or working inside of — hence, print working directory.
Here are some example outputs for the pwd
command for Windows, Mac, and Linux:
# Windows, using Git Bash
$ pwd
C:\Documents\
# macOs, using Z shell
$ pwd
/Users/me/Applications/
# Linux
$ pwd
/home/me/Documents
2. ls
: List
The ls
command will list all of the files and subdirectories in the current directory. Well, not all of them. There are many hidden files, especially in the home directory, that do not appear unless you add the -a
flag to ls
. Following are two examples from a typical Linux file system:
# Without showing hidden files (files that begin with a '.')
$ ls
Downloads Public "PlayOnLinux's virtual drives"
Code Music snap
Desktop package-lock.json Templates
Pictures Videos Documents
# Showing hidden files with the -a flag
$ ls
. .gitconfig .profile
.. .gnupg Public
.bash_history .local .quokka
.bash_logout .mongodb snap
.bashrc .mongorc.js .ssh
.cache .mozilla .sudo_as_admin_successful
Music .swt "PlayOnLinux's virtual drives"
Code .node_repl_history Templates
.config .npm .var
.dbshell package-lock.json Videos
Desktop Pictures .vscode
.pki .wallaby Downloads
Documents .PlayOnLinux .wget-hsts
# NOTE: The entries are normally displayed in alphabetical order.
# The above examples are from my personal computer,
# so I removed some entries, disrupting the order.
3. cd
: Change Directory
The cd
command is used to log to a
specified directory. Absolute or relative paths may be used. An absolute path begins with a ‘/’ forward slash), meaning the root directory, and can be referenced from any subdirectory. A relative path does not begin with a forward slash and must be accessible to the current working directory.
‘..
‘ (dot-dot) in a path means
“step out one directory” or “go up one level.” So if your current working directory is /home/me/Documents/LoveLetters
, running cd ..
will move you to /home/me/Documents
.
Below are several examples of using the cd
command:
# Going to Projects/MyApp/js individually
$ cd Projects
$ cd MyApp
$ cd js
# Or all at once
$ cd Projects/MyApp/js
# Step out once and go instead to the 'css' subdirectory
$ cd ../css
# Step out twice, up to the 'Projects' directory
$ cd ../..
# Now go to the 'Downloads' directory, using an absolute path
$ cd /home/me/Downloads
4. touch
This is the command with the most confusing name. touch
? What could that mean? Would you guess that it actually creates files?
Yes, it’s true. Here are some examples:
# Creating a single text file
$ touch some-text-file.txt
# Typical project setup. Creates all three files
$ touch index.html app.css script.js
# Creating files in other directories from outside those directories.
# Crazy, right?
$ touch js/script.js
$ touch css/app.css
$ touch ../../README.md
But why is it called touch
? Because its main purpose is actually not to create files but to “refresh” them, so to speak. Using touch
on an existing file will change the date it was last modified to the current date. Using it on a nonexistent file will create the file. But almost no one uses it for the first reason, so most people are left puzzled about how touch
somehow means create.
5. rm
: Remove
This command is the opposite of touch — it removes files. There’s not much else to say about rm, but when we get to rmdir we will revisit it. In the following examples, we will delete all of the files we created using touch.
WARNING: When using rm, be aware that the files will be permanently deleted! There is no Recycling Bin or Trash folder.
# Deleting a single text file
$ rm some-text-file.txt
# Deleting several files
$ rm index.html app.css script.js
# Deleting files in other directories from outside those directories.
$ rm js/script.js
$ rm css/app.css
$ rm ../../README.md
6. mkdir
: Make Directory
I’ll give you fourteen guesses on what this command does. Only fourteen!
All kidding aside, mkdir
does just what it sounds like — it makes directories.
# Making a single directory
$ mkdir MyApp
# Making subdirectories from outside
$ mkdir MyApp/css MyApp/js
7. rmdir
: Remove Directory
The rmdir
command removes directories, but there’s a catch: it can only remove empty directories. Don’t get mad at me, I didn’t make up that rule.
To remove a directory with any contents, you must userm
with two flags: r
for “recursive” and f
for “force.”
# To remove an empty directory, use rmdir
$ mkdir SomeUselessFolder
$ rmdir SomeUselessFolder
# But to remove directories with contents, use rm -rf
$ mkdir UsefulFolder
$ cd UsefulFolder
$ touch script.js some-dangerous-commands.sh
$ cd ..
$ rm -rf UsefulFolder
8. mv
: Move
The mv
command moves a single file to a specified destination.
# Make a directory, and move stuff.txt into it
$ mkdir RandomStuff
$ mv stuff.txt RandomStuff
9. cp
: Copy
cp
is used like mv
, but it copies files instead of moving them.
# Make a directory, and copy stuff.txt into it
$ mkdir RandomStuffBackup
$ cp stuff.txt RandomStuffBackup
10. alias
alias
makes our lives easier by allowing us to provide a shorter equivalent to
a long command.
For example, the command used to start up a MongoDB database
is sudo systemctl start mongod
. After a while, it becomes a real pain to type that every morning.
To create a shorter version, such as startmongo
, use the alias command as shown below.
$ alias startmongo='sudo systemctl start mongod'
# Now we can use the alias
# as a substitute for the longer version
$ startmongo
But there’s a catch: the alias normally only applies to the current terminal session. To make an alias permanent, you will need to add it to the end of your .bashrc
file then run the following command: source .bashrc
11. man
: Manual and -h
/--help
There are two main ways to get help for commands without using Google or Stack Exchange:
- Run
man
followed by the name of the command you need help for. The manual (often called the man pages) for the command will be displayed. When you’re done reading, press ‘Q’ to quit. - Run the command you need help for with either of the following flags:
-h
or--help
# To read the man pages for the touch command
# (I hope Colt Steele reads this)
$ man touch
# To display general help for a command
# (No 'Q' for quit necessary)
$ ls --help
Conclusion
The commands listed here are the most basic ones that you will find in any introduction to Bash. (Though the inclusion of alias
is a bonus that I rarely find in beginner’s guides despite its usefulness.) I have left out any tech-stack-specific commands such as node, npm, mongo, etc. These will depend on your preferences as a developer, those of your instructor, or those of your employer.
The best way to learn Bash is to start using it immediately. Take a break from using Finder or File Explorer, and use the command-line equivalents. Eventually, you will find that using the terminal is often faster than the familiar GUI methods!