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 starting point is an alternative to a window showing your files and folders, the command for this is…
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…
Change Directory to the path specified, for example cd projects. There are a few really helpful arguments to aid this:
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…
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…
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…
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…
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…
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…
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…
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:
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…
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 [[email protected]] [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.
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.
For more on how to use combinations of bash commands see:
- To boldly log: debug Docker apps effectively using logs options, tail and grep
- How I filter and grep Docker containers, images, and volumes and how you can too
Create your free account to unlock your custom reading experience.