Command Line Interface¶
Command Line Interface or CLI is the fastest way of communicating with a
computer. CLI can retrive data from the current system or run a task on a remote
server. Also, CLI provides a way to run periodic commands and runs long running
commands in the background, Daemons
,.
In this chapter, we will discover the import commands, and we will teach you to
use them. After opening up a terminal in Ubuntu, type in the following command
ls
. As you can see in the terminal, a list of files and directories showed
up. ls
is the command to list the contents of the current directory.
Important commands¶
As you might expect, anything that you can do in the GUI you can do in the command line. CLI has many Shells e.g. Bourne Shell, Korn SHell, Bourne_Again_SHell ...etc. Shells are program that help you execute commands on the computer. They have a special language, and they provide basic programming capabilities. Below are a list of important commands that you should be comfortable with.
- pwdStands for Print Working Directory:pwd prints the current working directory from
/
orroot directory
- mkdirMake Directory:
mkdir foo
creates a directory called foomkdir -p foo/bar/baz
creates baz and all the missing directories in the path to itExercise:- Make a directory called
temp
- Make a directory at the path
temp/stuff
- Make a directory at the path
temp/stuff/things
- Can you create a directory at
temp/stuff/things/frank/joe/alex/john
using a single command?
- Make a directory called
- cdChange Directory.Once invoked it will change your working directory to a new one:
cd temp
to change to the temp directory you created in the last exerciseExercise:- Change to the
temp
directory. Check where you are usingpwd
. - Change to the
stuff
directory. Check where you are. - Change directly to the
john
directory in one command. - Use the command
cd ..
. Where did you end up? - Where does
cd ../../..
take you? - What about
cd .
? - Just
cd
?
- Change to the
- lsLists the contents of the current directory:
ls -a
to list all files and directory including hidden directory ordotfiles
ls -l
to list the files and directory with more information about their permissions, owner, group that owns it, disk size and creation dateExercise:- Navigate back to your home directory
~
. - Go to
temp
and usels
to see what is in it. - While in
temp
tryls -lR
. What did it do? - Use a combination of
cd
,ls
andpwd
to explore the files on your machine.
- Navigate back to your home directory
- touchCreates an empty file in the current directory:
touch CaptainAwesomesauce.txt
creates a blank text file called CaptainAwesomesauce.txt - cpCopy file or directory from one location to another:
cp file1 file2
copies the contents of file1 into file2cp file1 Documents/
copies file1 into the Documents directorycp -r /tmp Documents/tmp
copies the contents of /tmp into Documents/tmpExercise:- Inside
temp
create a file callediamcool.txt
. - Make a copy of it called
awesome.txt
. - Make a directory called
stuff
and copyawesome.txt
into it. - Without leaving
temp
check the contents ofstuff
- Copy
stuff
and all its contents into a new directory calledthings
. - Without leaving
temp
check the contents ofthings
.
- Inside
- mvMoves files or directories to different location (path). Also it can be usedto rename files or directories:
mv file file.txt
renames file to file.txtmv Downloads/file.zip Documents/
moves file.zip from Downloads/ to Documents/Exercise:- Change the name of the file
awesome.txt
tonotawesome.txt
- Change the name of the directory
stuff
tofoo
- Move the file
iamcool.txt
fromtemp
intofoo
- Change the name of the file
Warning
Be careful when passing paths!
- Paths can be:
- Absolute Paths relative to roote.g.
/etc/init/
,~/Desktop/bar.py
- Realtive Paths from your current working directorye.g.
../foo/bar/
,temp/stuff/awesome.txt
- nanoNano is an easy to use terminal text editor:
nano file1
opens file1 for editing - lessLess is a file viewer, and it has search features. The name came from theUnix philosophy “Less is more, more is less”[1]:
less foo.txt
page through foo.txt - catConcatenate files and prints them to
stdout
:cat file1
spits the content of file1 tostdout
cat file1 file2
concatenates file1 to file2 then spits the contents tostdout
Exercise:Create a file called
zen.txt
with the following content:Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
View it using
less
andcat
. What’s the difference?
- rmRemoves a file or directory:
rm /path/to/file1
to delete file1rm -r /path/to/dir1
to recursively delete dir1 and all its contentsExercise:- Go to the
temp
directory - Remove the file
notawesome.txt
. - Remove the directory
things
and all its content.
- Go to the
- echoTakes a string of text and prints it to
stdout
echo Hello world
- |The Pipe character which takes the output of the left command and inputs it to the right command
ls | grep ""
- >Redirect to character; it redirect the output of the command to a file
echo Hello > foo.txt
- >>Append character; it appends the output to a file
echo Hello >> foo.txt
- <Input in character; it inputs the text of a file to the command
cat < foo.txt
- manReturn the help manual for any command in the system:
man shell-command
- findFind is a powerful command. Take a look at the manual of
find
to see allthe options that you can use with it:find . -type f -name foo
looks for a file that’s named foo - diffDifferences between two files. The command
diff
prints out the differencebetween two files:diff v1/foo1 v2/foo1
- commCommon is a command that compares two files and print the common bytesbetween them:
comm v1/foo1 v2/foo1
- headHead prints out first lines of a file:
head foo.txt
- tailTail is simliar to head but it prints out the last lines of a file:
tail foo.txt
- sortSort sorts text:
sort foo
- * - The Wildcard
*
is known as the wildcard because it matches everything.It’s great when you want to do a command on a set of files all at once:ls *.py
lists all the files in the current directory ending in.py
rm -r h*
removes all files and directories beginning withh
rm h*.*
removes only files beginning withh
Exercise:- Create the following files in
temp
: ex12.txt
ex13.txt
ex14.py
stupid.vb
useless.vb
wasteoftime.vb
- Create the following files in
List all the .txt files in
temp
.List all the files that begin with
ex
.Delete all the
vb
files!Use
find
andless
to see all the.txt
files under yourhome
directory.
Hint
You will need a |
pipe for that last exercise
- grepGrep is a pattern search that uses regular expressions to look for a patternin text. It’s powerful if you know regular expressions:
grep this words.txt
looks for the wordthis
inside a file namedwords.txt
Exercise:Create a file in
temp
callednewfile.txt
with the following text:This is a new file. This is a new file. This is a new file.
Create another file called
oldfile.txt
but with:This is an old file. This is an old file. This is an old file.
Search for all occurences of the word
new
in all the.txt
files intemp
.Search for all occurences of
old
.Search for all occurences of
file
.How would you search for the words
This is
?
Hint
You can quickly type text into a file using cat > file.txt
This will overwrite file.txt
with whatever you type until you close the file using CTRL-c
.
See also
Take a look at the Python Docs for more information
- envPrints out all the environments variables
env
Exercise:- Print all your environment variables.
- Use
|
andgrep
to print only the variables that have your username in them.
- exportExport a local varialble to become an environment variable
export VAR
Exercises:- Create an environment variable called
TESTING
and set it to"1 2 3"
. echo
your new varialble.
- Create an environment variable called
Note
Environment variables are reset every time a new terminal session starts.
- sshSecureShell is a program that connects you to remote computers and executecommands on them:
ssh alice@foo.com
- scpSecure copy like FTP but uses SSH protocol to transmit data:
scp words.txt alice@foo.com:Desktop/store
- sudoSuper User DO is a command that escalates and runs the given command as root
- `` or ${}Backticks command; which executes the command inside it and returns the output:
cat `ls *txt`
- ifconfigTo check the network cards and the ip address
- aliasTo alias command and modify them
alias l="ls -al"
See also
Here is a comprehensive Command Line Cheatsheet
Dot files/directories¶
Dot files and directories play a big role in the Unix/Linux operating system. Once a file or directory starts with a . it will be hidden from the regular ls command, and to display it you need to run ls with -a flag. The flag display the all the files in the current directory. There are many special dotfiles that you need to be aware of. The list below lists couple of them.
- ~/.bashrc
- ~/.vimrc
- ~/.emacs
- ~/.bash_history