visit
pwd
—shows the full path of your working directory:
cd <folder>
—switches the working directory to <folder>
:
A few more examples with cd
:
cd ..
—moves to the parent folder of the current onecd -
—moves back to the preview foldercd ~
—moves to home directory
So, now that we know what folder we are in, let’s now see what we have inside. The command to show a list of files is ls
:
As you see, both files and directories are shown together, and it’s not entirely clear what is what. I usually run this command with two parameters:
-a
—to show all files, event the hidden ones-l
—to show the information in long formatIn the output you can see:
.
& ..
—aliases to the folder itself and its parent folder.git
—hidden folder for keeping the files used by a Git repository-
or d
. The first means files; the second means directories.touch <file-name>
—creates an empty filemkdir <directory-name>
—creates an empty folderrm <file-name>
—removes the specified filerm -d <directory-name>
—removes the specified folder
man
is a command that shows you a manual for other commands. All the commands we have mentioned so far have plenty of options. You can learn more about them directly from the terminal by using man
. You run man <command name>
:
echo
is a simple program that returns its arguments to the standard output. You can use it to write a value to a file:
echo Test > test.txt
cat
a tool mean to concatenates multiple files. It’s often used to pick the content of only one file, to get it to the standard input of another command with the pipe operator. Example:
cat file-1.txt file-2.txt
In the context of CLI, wc
stands for word count. It lets you check the length of the output of other files:
The output here means that in the specified file we have:
head
and tail
are utilities to read a few lines from the beginning or end of the files, respectively. By default, both show 10 lines. You can see them in action:
less
is a command that paginates a file or its input to fit it on the screen. I covered the basic navigation shortcuts in :
You can use less
to view the output of any other command—just connect with a pipe:
grep
is a great search tool that supports . Its power comes from flexibility: by specifying one of its numerous options, you can tweak its behavior to what you need. By default, it waits for values to come in the standard input—not the most common use case. Therefore, you most likely want to run it as a filter inside a command pipeline.
In this pipeline:
cat *txt
—concatenates all files in the current folder that ends with txt and returns it in a standard outputgrep 1
—returns only lines that have 1
in themOften, it’s easier to describe what you don’t want to have rather than what you want. With grep
, you can easily use it to filter out some lines by adding the -v
option to it. For example:
Note! You can combine many commands with the pipe operator:
cat *txt | grep -v 1 | grep file-
Content of file-2.txt
Grep allows you to search in files as well—you just need to add -r
(from recursive search in subdirectories):
In the output, you can see:
./file
—path to the fileLorem ipsum
—the matching lineIf you are interested only in which files have your matching values, then you can add the -l
option to your command:
This option becomes especially useful when you combine it with the next command/
xargs
takes lines from standard input and adds them as parameters for another command. One of the common uses is to find all files that contain some value and run a command on them—for example, delete them:
grep -rl Lorem | xargs rm
There is no output of the command, but let’s see what files are available before and after running the command:
ls
long-file.txt
For now, the files are gone, but later in this article we will see a tool that helps us track and restore files.
As you can see in the second run of ls -R
(recursive listing of a directory and each subdirectory):
file
is gone from the top directory, andanother-file.txt
is gone from the nested folder.sed
is a stream editor. It allows for modifications to the values in the pipe:
echo 'hello world' | sed 's/world/you/'
hello you
In our example, the parameter s/world/you/
denote the following:
s
—means substituteworld
—regex to match the value to be substitutedyou
—new value to be used insteadA more practical use case for sed
is to edit files. When can combine it with grep
and xargs
, you can start developing pretty neat one-liners that will help you apply big updates to your codebase:
# at Linux
grep -lr file | xargs sed -i -e 's/file/File/g'
# at macOS
grep -lr file | xargs sed -i '' -e 's/file/File/g'
In our command:
grep -lr file
—returns the list of all files that contains file
.xargs
—turns them into input parameters for sed
.sed -i -e
or sed -i '' -e
—switches the editor to work directly on the files.s/file/File/g
—substitutes lowercase letter “file” with uppercase “File”. /g
at the end means that the change is meant to be global. It should update every instance that is found in the file—not only the first instance in each line.This is a command inspired by grep
. It lets you search through the files tracked by the repository. When you track your files with Git, you can use git grep
instead of grep -r
. The main advantage is that it automatically ignores files that you set Git to ignore—which is often the case with node_modules
.
Similar to the original grep
, you can use -l
option to get only the file names:
$ git grep -l File
file-a
file-b
file-c
A command to list all files tracked by the repository. You can combine it with grep
to find all the files that match some pattern, and then use xargs
to do some operation on all of them.
vim
is a text editor that is a bit of a nerdy cliché, and my editor of choice. I switched to it after I got tired with an old computer that struggled to run Eclipse and other big integrated development environments (IDEs).
Vim is fast, flexible, and offers great keyboard shortcuts. Even more: keyboard shortcuts are the only way to use it because it doesn’t support a mouse. The learning curve is a bit steep—at first it can be challenging to even close it. Long term, learning Vim can be a good investment. What makes it especially worthwhile is that the editor commands in Vim are very close to the commands that are used by sed
and search patterns in greps
. What you learn in one of those tools, you can immediately apply in the other tools as well.
pbcopy
—utility that is available on macOS
xclip
—utility that you could install on Ubuntu
tmux
is a terminal multiplexer—a program that allows you to run multiple sessions inside one terminal emulator window. It allows you to keep different ‘windows’ for different uses. For example, I usually have: