Top 10 Bash file system commands you can’t live without

Learning command line tools for managing the file system in Bash or alternative shells like, Zsh, can give you more speed and control over your workflow. Here is a low down of a top 10 to use — or to try for the first time.

The Top 10

The starting point is an alternative to a window showing your files and folders, the command for this is…

1) ls

Lists the folder and file names in the current working directory. You can narrow the list down to files matching a specific name by passing the name as a parameter. This comes with a variety of flags; a few key ones are -l to list the long format that displays the file permissions, and -a to list all files including hidden files.

So now you can see your files and folders, to navigate to a different directory you use…

2) cd

Change Directory to the path specified, for example cd projects. There are a few really helpful arguments to aid this:

  • . refers to the current directory ./projects
  • .. can be used to move up one folder, use cd .., and can be combined to move up multiple levels ../../my_folder
  • /is the root of your system to reach core folders, such as system, users, etc
  • ~ is the home directory, usually the path /users/username. Move back to folders referenced relative to this path by including it at the start of your path, for example ~/projects . If you need to access your shell configuration, you can easily find this with ~/.bashrc or ~/.zshrc — then add all sorts of beneficial aliases, configurations, commands and paths.
For more on the mac directory structure
- OSXDaily: Mac OS X Directory Structure Explained

Now you can navigate directories, its time to create some of your own using…

3) mkdir

Make directories with this command mkdir my_folder . Not only can it make the folder specified, but also its parents if they do not exist already using the -p option. The command mkdir -p first_folder/next_folder/my_folder would also create the first and next folder. There is an option to set the mode, or permissions, via the -m flag, and these can be changed later with the chmod command (see below for more on mode and permissions).

There are various ways to then create files using the command line, one of the most common is…

4) touch

Touches the file to update the access and or modification date of a file or directory without opening, saving or closing the file. But one of the most common uses is to create an empty file touch my_file.

Before you ask why I suggested touch to create files, read this page, which explores ways to create files using shell commands
- StackExchange Unix & Linux: Why isn’t there any shell command to create files?

To then use the file, open my_file will launch the application set for this file type. But you might want to see what’s in your files within the scope of the terminal. To do this you can use…

5) cat

Concatenate and print files to stdout cat my_file. You can pass one or more file names to this command, and even number the lines using the -n to number the lines. A close cousin of this is vi to launch a terminal-based text editor.

For more on ways to use the cat command
- 13 Basic Cat Command Examples in Linux

The files may not be in the place you want them to be, rather than drag and drop using the mouse and windows, you command your terminal to…

6) mv

Moves files and folders. The first argument is the file you want to move, and the second is the location to move it to. Use the flags -f to force move them and -i to prompt confirmation before overwriting files.

Alternatively, you may not want to remove the original, for example when making backups, in this case you would use…

7) cp

Copies files and folders cp my_file ./projects . The flag -r recursively copies subfolders and files.

Now you have too many files, or some of them are no longer needed. The next command is a danger zone, because it is so good at its job…

8) rm

Removes files and folders rm my_folder . Using -r will again recursively delete subfolders, -f force deletes, and -rf for a recursive force delete. If you want to remove all folders and files in the current directory the command is rm -rf ./* , if you leave out the dot then it would reference the root directory!

Want to see what happens if you force a computer to run rm -rf /? Tip: don’t try this on yours, instead watch this video:
- What happens when you sudo rm -rf / your machine? — YouTube

There are ways to stop accidental deletes. If you use the -i flag (for interactive) you can specify the computer to prompt confirmation before delete.

Another way is with permissions, which can be modified…

9) chmod

Change mode so you can set permissions for read, write and execute for the user, members of your group and others. This uses binary values as an argument to set these. There are many common chmod permissions, a few key ones are:

  • 777 — anyone can read, write and execute chmod 777 my_file
  • 755 — for files that should be readable and executable by others, but only changeable by the issuing user
  • 700 — only the user can do anything to the file
For a more lengthy and detailed explanations of chmod see
- ComputerHope: Linux Chmod Command

If you get stuck or ever need more information without resorting to the internet, ask for some help from…

10) man

Manuals for a command can be shown with this instruction. Below is some of the output from running man ls, it also displays all the options available for running the command.

LS(1)                     BSD General Commands Manual                    LS(1)
NAME
ls -- list directory contents
SYNOPSIS
ls [-ABCFGHLOPRSTUW@abcdefghiklmnopqrstuwx1] [file ...]
DESCRIPTION
For each operand that names a file of a type other than directory, ls displays its name as well as
any requested, associated information. For each operand that names a file of type directory, ls
displays the names of files contained within that directory, as well as any requested, associated
information.

Find out more

There are many other commands to learn and get to grips with. There are commands to manage processes (ps, kill), text processing (echo, printf, sed), and searching (find, grep). To make even better use of your shell or scripts commands can be combined or piped to send output from one command to another.

If you like illustrated tutorials, I really recommend checking out Bite Size Command Line by Julia Evans.

Read more from ryanwhocodes

How to use combinations of bash commands

Some of my other Bash tutorials