ls
- list files
ls -a
- display hidden files and folders
ls -R
- provide a tree listing of directory structure.
ls -l
- displays extra details like size, user, date, permissions
cd <path>
-> change directory
(cd ..
-> takes to parent directory
cd
-> takes to user directory[denoted by ~]
cd -
-> takes to the previous directory we were in)
- Trivia:
user
directory is present in thehome
directory which is present in theroot
directory. Then there is no upper directory.
pwd
- print working directory
^
symbol is used for representing 'ctrl' ... e.g. ^C
= ctrl key + c
touch <filename>
- creates a new file with any extension we want.
rm
- used to remove a file.
rm -r
- used to remove a directory.
mkdir
- makes a directory
rmdir
- removes an empty directory
cp /loc1/file /loc2/file
- used to copy file from /loc1 to /loc2.
mv /loc1/file /loc2/file
- used to move file from /loc1 to /loc2.
- Trivia:
mv oldfilename newfilename
is the best method to rename a file.
grep <text to search> <files to search in>
cut
- cuts out selected portions of each line from file and writes them to the standard output.
cut -c 2-5 file
- cut characters 2 to 5 from each line of file
cut -d"x" -f 1 file
- returns each part of every line before first occurance of 'x' (-d is delimiter and -f is field)
clear
or ^l
- scrolls down to an empty screen
reset
- change terminal to default settings
find <query>
- finds files and directories with name query
in the current directory and its subdirectories
find -d <query>
- looks for a directory with name query
find -f <query>
- looks for a file with name query
history
- shows all typed commands history
history n
-shows last n commands
sudo <command>
- Run the command as superuser.
!<text>
- repeats a previous command in history which started with 'text'
!!
- repeats the previous command
sudo !!
- repeats the previous command as superuser.
whoami
- gives the username of the current user.
sudo su <username>
- Used to switch to a different user. This prompts for the password of the user you switch to.
- Trivia:
sudo
stands forSUperuser DO
andsu
stands forSwitch User
.
man <command>
- shows manual entry for the command. Manual contains all the flags realated to that command and their use.
time <command>
- gives the time taken for the command to execute. Very useful when you want to find the execution time of your programs.
diff [file1] [file2]
- compares the two files line by line. Usually used to compare ideal output and user-generated output.
diff -z [file1] [file2]
- Ignores trailing-whitespaces while comparing the two files
.
- refers to current directory
..
- refers to parent directory
- Trivia: Files with file names starting with
.
are hidden.
echo "hello"
- prints hello on the terminal
>
- used to store the output of a task in some file(overwrites if file with same name is present) rather than displaying it on the terminal.
>>
- same task as >
but does not overwrites just appends to the file with the same name.
|
- piping is used to give the output of a command as input to another command ..for e.g history | grep "find"
will search for "find" in the output of history
<command> | tee <filename>
- used to show output of command
on terminal as well as writing the output to a file filename
.
cat
- used to open a file in terminal in read-only format
- Trivia: Unless you have infinite scrolling turned on (available in Profile Preferences -> Scrolling tab of the terminal), there is a limit to how many lines you can see on the screen.
<output of some command> | less
- allows the user to advance through the content by pressing SPACE, move backwards by pressing 'b' and quit using 'q'. Pressing ESC followed by SPACE allows you to scroll down one screen at a time.
Example: cat file | less
<output of some command> | more
- is similar to using less
, but allows viewing one screen at a time.
- Trivia: All commands typed in the terminal are saved in
history
or the.bash_history
file in the home directory.history | less
orcat ~/.bash_history
will let you scroll through previously typed commands.
-f
forcefully do a task i.e. without asking for confirmation
-r
recursively do a task(looks in subdirectories too)
-o <filename>
- stores the output with a personalised filename rather than the default filename. For e.g. g++ -o myfile program.cpp
will generate an executable with the name myfile
rather than the default a.out
.
<vim|vi|nano|emacs> <filename>
: opens a file in the respective text editor inside the terminal.
gedit <filename>
: opens a file with filename in Gedit .
subl <filename>
: opens a file with filename in Sublime Text.
subl <foldername>
: Opens the entire folder in Sublime Text. Very helpful when you are working on projects with multiple file.
sh myscript
- To run a non-executable sh
script.
bash myscript
- To run a non-executable bash
script
./<location/of/executable>
- Just type the file location to run an executable file.
An alias is a word assigned to a statement, and acts as a keyboard shortcut.
alias py='python'
- would pass python
whenever py
is entered.
This alias lasts as long as the terminal is running. To create a permanent alias, append this line to ~/.bash_profile
or ~/.bash_aliases
.
unalias <alias_name>
- Removes the alias. E.g. unalias py
- After this py
would not work as python
.
Wget
and cURL
are two great utilities for downloading stuff. They are a replacement to the Download Managers you must have used on Windows.
- Trivia:
cURL
is powered bylibcurl
- a cross-platform library with a stable API that can be used by each and everyone.Wget
on the other hand is command line only. There's no library.
wget <url_to_download>
- Downloads the file at the specified url.
wget -c <url_to_download>
- Resumes an incomplete download. Very helpful when a large file download stops due to some error.
wget --tries=100 <url_to_download>
- Set the retry download attempts. This is very useful when the download file is large and the internet connection has problems.
- Trivia:
wget
does 20 retries by default.
wget -i <download_list_file.txt>
- For Multiple downloads. Downloads all the files/URLs mentioned in file.
wget --recursive --page-requisites --html-extension --convert-links --no-parent <URL>
- Use this command to download the entire website so that you can view it offline.
-
--recursive: download the entire Web site.
-
--page-requisites: get all the elements that compose the page (images, CSS and so on).
-
--html-extension: save files with the .html extension.
-
--convert-links: convert links so that they work locally, off-line.
-
--no-parent: prevents wget from downloading anything from the folders beneath the folder you want to acquire.
-
Trivia:
cURL
supports more protocols and authentication methdods thanWget
and is almost always pre-installed on the OS.Wget
on the other hand is famous because of its ability to download an entire website for offline view.
curl -O <url_to_download>
- Downloads the file at the specified url.
curl -O <URL1> -O <URL2>
- Downloads files at both urls.
curl -C - -O <url_to_download>
- Resumes an incomplete download.
- Trivia:
cURL
can also be used to upload files toftp
server. Usecurl -u <ftpuser>:<ftppass> -T <myfile> <ftp://ftp.testserver.com>
chmod a+x file
- Grants execution permission to all users of a file.
chmod a+w file
- Grants write permission to all users of a file.
chmod a+r file
- Grants read permission to all users of a file.
This are just examples. chmod
has a lot of different configurations for different kinds of permissions. For all details see its man
page.
chown -R <username> path/of/file/or/directory
- Gives the ownership of the file or all files in the directory and its subdirectories to the mentioned user.
ifconfig
- when used without any flags, used to display the status of all active network interfaces.
iwconfig
- similar to ifconfig
, but used for wireless network interfaces.
ping [domain_name_or_ip_address]
- Used to ping a domain name or IP address continuously. It can be stopped by ^C
. Generally used to check if the server is up and responding.
dig example.com
- Queries DNS servers for information. Gives back the A
record which points the domain name to an IP address. Using the +short
flag returns just the IP address linked to the domain name.
+nocomments
– Turn off the comment lines
+noauthority
– Turn off the authority section
+noadditional
– Turn off the additional section
+nostats
– Turn off the stats section
+noanswer
– Turn off the answer section (Of course, you wouldn’t want to turn off the answer section)
dig -x [IP address]
- Queries and returns a PTR
record against the IP address queried. The PTR record helps in Reverse DNS Lookup i.e. it provides the domain name linked to an IP address. Example dig -x 127.0.0.1 +short
returns localhost.
.
arp
arp manipulates or displays the kernel's IPv4 network neighbour cache. It can add entries to the table, delete one, or display the current content.
traceroute [IP address]
tracks the route packets taken from an IP network on their way to a given host. It utilizes the IP protocol's time to live (TTL) field and attempts to elicit an ICMP TIME_EXCEEDED
response from each gateway along the path to the host.
whois domain_name.com
- Generates a long list of output regarding the server registration.
tar -xvzf <file.tar.gz>
- used to extract the .tar.gz file
tar -cvzf <tarballname.tar.gz> <item_to_compress_1> <item_to_compress_2>
- used to compress any number of files into a .tar.gz compressed archive.
-tarball.tar.gz: This is the name of the final compressed archive.
-x: tar can collect files or extract them. x does the latter.
-c: Collects files to be compressed
-v: makes tar talk a lot. Verbose output shows you all the files being extracted.
-z: tells tar to decompress the archive using gzip
-f: this must be the last flag of the command, and the tar file must be immediately after. It tells tar the name and path of the compressed file.
top
- displays processor activity in real time.
ps
returns the snapshot of current processes.
ps -e
returns every process running on the system
ps -u <useraccount>
returns list of processes running on user account.
ps -u <useraccount> | grep <Application>
- fetches all processes of "Application"
The left most number returned by the ps
command is called the Process ID (PID).
A particular process can be terminated using kill
kill <PID>
- kills the process having PID as that entered.
kill -9 <PID>
- performs a violent kill
killall <processname>
- kills all instances of processname