I found the linux command line quite intimidating when I first started learning to code. It seemed to require extensive and comprehensive knowledge and understanding of commands and keyboard shortcuts. However, as I read more about it and practiced what I learned, I realized that it isn't as scary as it seemed; that It only requires a lot of constant practice. I also discovered that others feel the same way, which made me realise that I wasn't alone.
The other thing I discovered during this period is that we are all beginners and sharing is helpful, since no one is an island. So I decided to share my experience with others starting out as well. As we all know, learning is never-ending. There will always be new tools and new commands to learn, especially when you're a beginner. The following tips will help you navigate through files and directories on the command line. 👇
Manipulating Files and Directories
to print a string of characters to your screen, use the
echocommand by typing 👉
echo Hello world. To print without a newline being inserted, use the
-noption as follows; type 👉
echo -n <string>. To print a string of characters to a file without using a text editor, use the redirect operator
>by typing 👉
echo "string" > filenamee.g.
echo "this prints to file" > index.html. To add a new string of characters to the next line of same file, use the append operator
>>and type 👉
echo "string" >> filename.
to dump the contents of a file to your screen, use the
catcommand and type 👉
cat book.txt. To facilitate the comparison of files that are similar but not identical, use the
diffcommand, type 👉
diff <filename1> <filename2>. Note that when there is no difference between two files,
diffsimply outputs nothing. To dump the contents of a file (or to combine the contents of multiple files) into a separate one, direct the output of the
catcommand to the new file using the redirect
>operator, type 👉
cat filename(s) > newfile.
to abort the current task and regain user-control of the terminal, press 👉
Ctrl-C. If this command fails, hit the
Esckey. To be able to move quickly within the command line, press 👉
Ctrl-Ato get to the beginning of the line;
Ctrl-Eto get to the end of the line and;
Ctrl-Uto clear the entire line and start over.
to learn more details about a command, type 👉
man <command name>e.g
man cat. Note that
manpages use the same interface as the
lesscommand so you can navigate through both using the same key shortcuts. To open a new terminal tab (or window), press 👉
Ctrl+Shift+Nrespectively. To clear your screen, type 👉
clearor press 👉
Ctrl-L. To exit a terminal window (or tab), press 👉
Ctrl+Shift+Wor type 👉
to run the previous command exactly as written, use the exclamation point
!(pronounced bang) and type
!!. Another way to repeat previous commands is by typing
!followed by a character (or number of characters), which runs the last command that started with those characters. For example, to run the last
lscommand issued, type 👉
! l. Another powerful technique is to press 👉
Ctrl+R. This allows you to search interactively through your previous commands, and then optionally edit the result before executing.
to create a hard link to a file; use the
lncommand by first typing the name of the file you want to link to (i.e the source file), followed by the name of the linked file you want to create (i.e the target) for example 👉
ln letter.doc book.doc. To force a link (say, to an existing file) (or to execute a command without having to confirm it) use the
ln -f letter.doc index.html.
The default type of link that gets created when using the
lncommand is the hard link. Hard links create an identical copy of the linked file on disk, that gets updated automatically as the source file is updated. However, this type of link does not work for directories.
to create a link to a directory, use the
-sflag to create a symbolic link. This flag can also be used for linking to files as well, not just directories for example,
ln -s letter.doc index.html. Symbolic links can also link to files or directories on other file systems. File systems refer to directories and files.
to open a file or a directory or access a URL, type 👉
xdg-open <filename>/<directory>/<URL>. To download a file from the internet, use the
curlutility which allows you to interact with URLs at the command line; type 👉
curl -OL <URL>. To fetch the HTTP header of a site, type 👉
curl -I <URL>.
to view the beginning and end of a file, use the
tailcommands by typing 👉
tail <filename>respectively. They show the first and last 10 lines of the file, as aplicable. To print the first n lines of a file (instead of the first 10), type 👉
head -n <number> <filename>.
to count the number of lines in a file, type 👉
wc <filename>. The output shows three separate figures, indicating the number of lines, words, and bytes in the file. To view a file that is actively changing, type 👉
tail -f <filename>. This command is mostly executed when monitoring files used to log the activity of web servers for instance, in a practice known as 'tailing the log file'.
to easily navigate through the contents of a large file, use 👉 the
lesscommand for example type 👉
less <filename>. While in
lessmode; press the
Ctrl+Fto move forward a page; the
arrow keysto move one line up or down;
Ctrl+Bto move a page up; press 👉
Gto move to the beginning and end of the file respectively (to go directly to a specific line, type 👉
<linenumber>G); to search through the file for a string/word, use the forward slash key
/, e.g. type 👉
/<word>; press 👉
nto move to the next search result and
Nto the previous search result and to quit the
lesscommand, press 👉
to search directly for a word/string in a file, use the
grepcommand, type 👉
grep <word> <filename>. To search for a word/string in a file when you aren't sure where the file is, use the
-rflag and type 👉
grep -r <word>.To perform case-insensitive search using
-iflag by typing 👉
grep -i <word> <filename>. To exclude a word/string from a search term when using
grep, use the
-voption as follows 👉
grep <search term> <filename> | grep -v <word>.
to find the line number(s) in a file where a word appears, type 👉
grep -n <word> <filename>. To print the first 'n' lines of a search result, pipe to the
headcommand as follows 👉
grep -i <word> <filename> | head <-n>. To count the number of lines containing references to a search term/string, use the pipe
|and word count
wccommands as follows 👉
grep <word> <filename> | wc.
to print the history of commands you have previously executed in your terminal shell, use the
historycommand and pipe
lessas follows 👉
history | less. To execute a specific command in your command history, type 👉
!nwhere n represents the command number e.g. the 43rd command in your history.
Lastly, to modify system files or directories and execute tasks as root, use the
As always, thanks for reading! 👋 👋
Top comments (0)