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 foocreates a directory called foomkdir -p foo/bar/bazcreates 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/johnusing a single command?
- Make a directory called
- cdChange Directory.Once invoked it will change your working directory to a new one:
cd tempto change to the temp directory you created in the last exerciseExercise:- Change to the
tempdirectory. Check where you are usingpwd. - Change to the
stuffdirectory. Check where you are. - Change directly to the
johndirectory 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 -ato list all files and directory including hidden directory ordotfilesls -lto 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
tempand uselsto see what is in it. - While in
temptryls -lR. What did it do? - Use a combination of
cd,lsandpwdto explore the files on your machine.
- Navigate back to your home directory
- touchCreates an empty file in the current directory:
touch CaptainAwesomesauce.txtcreates a blank text file called CaptainAwesomesauce.txt - cpCopy file or directory from one location to another:
cp file1 file2copies the contents of file1 into file2cp file1 Documents/copies file1 into the Documents directorycp -r /tmp Documents/tmpcopies the contents of /tmp into Documents/tmpExercise:- Inside
tempcreate a file callediamcool.txt. - Make a copy of it called
awesome.txt. - Make a directory called
stuffand copyawesome.txtinto it. - Without leaving
tempcheck the contents ofstuff - Copy
stuffand all its contents into a new directory calledthings. - Without leaving
tempcheck the contents ofthings.
- Inside
- mvMoves files or directories to different location (path). Also it can be usedto rename files or directories:
mv file file.txtrenames file to file.txtmv Downloads/file.zip Documents/moves file.zip from Downloads/ to Documents/Exercise:- Change the name of the file
awesome.txttonotawesome.txt - Change the name of the directory
stufftofoo - Move the file
iamcool.txtfromtempintofoo
- 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 file1opens 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.txtpage through foo.txt - catConcatenate files and prints them to
stdout:cat file1spits the content of file1 tostdoutcat file1 file2concatenates file1 to file2 then spits the contents tostdoutExercise:Create a file called
zen.txtwith 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
lessandcat. What’s the difference?
- rmRemoves a file or directory:
rm /path/to/file1to delete file1rm -r /path/to/dir1to recursively delete dir1 and all its contentsExercise:- Go to the
tempdirectory - Remove the file
notawesome.txt. - Remove the directory
thingsand all its content.
- Go to the
- echoTakes a string of text and prints it to
stdoutecho 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
findto see allthe options that you can use with it:find . -type f -name foolooks for a file that’s named foo - diffDifferences between two files. The command
diffprints 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 *.pylists all the files in the current directory ending in.pyrm -r h*removes all files and directories beginning withhrm h*.*removes only files beginning withhExercise:- Create the following files in
temp: ex12.txtex13.txtex14.pystupid.vbuseless.vbwasteoftime.vb
- Create the following files in
List all the .txt files in
temp.List all the files that begin with
ex.Delete all the
vbfiles!Use
findandlessto see all the.txtfiles under yourhomedirectory.
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.txtlooks for the wordthisinside a file namedwords.txtExercise:Create a file in
tempcallednewfile.txtwith the following text:This is a new file. This is a new file. This is a new file.
Create another file called
oldfile.txtbut with:This is an old file. This is an old file. This is an old file.
Search for all occurences of the word
newin all the.txtfiles 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
envExercise:- Print all your environment variables.
- Use
|andgrepto print only the variables that have your username in them.
- exportExport a local varialble to become an environment variable
export VARExercises:- Create an environment variable called
TESTINGand set it to"1 2 3". echoyour 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