Hey there π π I write articles aimed at newbies like me!
echo
command by typing π echo <string>
e.g. echo Hello world
. To print without a newline being inserted, use the -n
option as follows; type π echo -n <string>
. To print a string of characters to a file without using a text editor, use the redirect operator >
. π echo "string" > filename
e.g. echo "this prints to file" > index.html
.>>
. π echo "string" >> filename
. To dump the contents of a file to your screen, use the cat
command: π cat <filename>
e.g cat book.txt
. To facilitate the comparison of files that are similar but not identical, use the diff
command: π diff <filename1><filename2>
.diff
simply 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 cat
command to the new file using the redirect >
operator:cat filename(s) > newfile
.Ctrl-C
. If this command fails, hit the Esc
key. To be able to move quickly within the command line, press π Ctrl-A
to get to the beginning of the line; Ctrl-E
to get to the end of the line and; Ctrl-U
to clear the entire line and start over.man <command name>
e.g man cat
.man
pages use the same interface as the less
command so you can navigate through both using the same key shortcuts. To open a new terminal tab (or window): π Ctrl+Shift+T
and Ctrl+Shift+N
respectively. To clear your screen: π clear
or Ctrl-L
. To exit a terminal window (or tab): π Ctrl-D
or Ctrl+Shift+W
or type π exit
.!
(pronounced bang) and type !!
. Another way to repeat previous commands is by typing !
followed by a character (or a number of characters), which runs the last command that started with those characters. For example, to run the last ls
command issued, type π ! l
. Another powerful technique is to enter π Ctrl+R
. This allows you to search interactively through your previous commands, and then optionally edit the result before executing.ln
command 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 -f
flag ln -f letter.doc index.html
.ln
command 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.-s
flag 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.xdg-open <filename>/<directory>/<URL>
. To download a file from the internet, use the curl
utility which allows you to interact with URLs on the command line; π curl -OL <URL>
. To fetch the HTTP header of a site, type π curl -I <URL>
.head
and tail
commands by typinghead <filename>
and tail <filename>
respectively. They show the first and last 10 lines of the file, as applicable. To print the first n lines of a file (instead of the first 10):head -n <number> <filename>
.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'.less
command for example type π less <filename>
. While in less
mode; press the spacebar
or Ctrl+F
to move forward a page; the arrow keys
to move one line up or down; Ctrl+B
to move a page up; press π 1G
and G
to 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 π n
to move to the next search result and N
to the previous search result and to quit the less
command, press π q
.grep
command: π grep <word> <filename>
. To search for a word/string in a file when you aren't sure where the file is, use the -r
flag: π grep -r <word>
. To perform a case-insensitive search using grep
use the -i
flag by typing π grep -i <word> <filename>
.grep
, use the -v
option as follows π grep <search term> <filename> | grep -v <word>
. To find the line number(s) in a file where a word appears: π grep -n <word> <filename>
. To print the first 'n' lines of a search result, pipe to the head
command as follows π grep -i <word> <filename> | head <-n>
.|
and word countwc
commands as follows π grep <word> <filename> | wc
. To print the history of commands you have previously executed in your terminal shell, use the history
command and pipe |
it to less
as follows π history | less
. To execute a specific command in your command history, type π !n
where n represents the command number e.g. the 43rd command in your history.sudo
command to modify system files or directories and execute tasks as root.