There are a lot of basic Linux commands. In this article, I will show you basic commands with their extended usage.
# System Info
date
– show current date and time.
cal
– show this month’s calendar.
uptime
– show current uptime.
w
– display who is online.
whoami
– who you are logged in as.
finger <user>
– display information about the user.
uname -a
– show kernel information.
cat /proc/cpuinfo
– CPU information.
cat /proc/meminfo
– memory information.
man <command>
– show the manual for command.
df
– show disk usage. -h
more human-friendly values.
du
– show directory space usage.
du -hsx * | sort -rh | head -10
– show directories, in human-readable form, sort from biggest to lowest, summarize folder sizes, and show top 10 positions.
free
– show memory and swap usage.
whereis <app>
– show possible locations of the app. -b
-binaries,-m
-manual section, -s
-source
which <app>
– show which app will be run by default.
lsb_release -a
– distribution information, -r
-release number,-c
-codename.
# File Commands
ls -F
– show indicators after each entry, slash if, for the folder, an asterisk is an executable file, at-sign is for the alias.
ls -l -h
– translate file sizes to more human-friendly notation.
ls -t
– sort by time.
ls -m
– comma-separated.
ls -R
– list with recursion.
pwd
– show current directory.
mkdir
– create directory.
rm
– delete a file.
rm -r
– delete directory.
rm -f
– force remove.
cp
– copy file.
cp -r
– copy directory.
mv
– move file.
ln -s
– create a symbolic link.
ln -s python_script.py /usr/bin/command
– creating user command that will execute python script and will be recognized globally by the system.
touch <file>
– create file.
cat > file
– create file, you can write something to save in that file and to save hit Ctrl+D
.
cat <file>
– display file content.
cat -n <file>
– show line numbers.
more
– display file content.
head
– display the first 10 lines of the file.
tail
– display the last 10 lines of the file.
tails -f
– keep showing the last 10 lines of the file as it grows. Track changes.
tail -n 15 -f access.log
– display and track changes for the last 15 lines of the log file.
tail -f access.log | grep 127.0.0.1
– display and track changes for a specific value (in this example IP 127.0.0.1) in an access log file.
watch tail -n 15 access.log
– display the last 15 lines of the log file and update output every 2 seconds.
watch -n 10 tail -n 15 access.log
– display and track change every 10 sec for the last 15 lines.
wc -l <filename>
– count lines in the file.
# File Permissions
chmod <octal> <file>
– change the permission of the file to octal, which can be found separately for users, groups, and the world by adding:
- 4 — read (r)
- 2 — write (w)
- 1 — execute (x)
chmod 777 file
– read, write, execute for all to file.
# Compression
tar cf <file.tar> <files>
– create a tar named file.tar containing files.
tar xf <file.tar>
– extract the files from file.tar.
tar czf <file.tar.gz>
– create a tar with Gzip compression.
tar xzf <file.tar.gz>
– extract a tar using Gzip.
tar cjf <file.tar.bz2>
– create a tar with Bzip2 compression.
tar xjf <file.tar.bz2>
– extract a tar using Bzip2.
gzip <file>
– compresses the file and renames it to file.gz.
gzip -d <file.gz>
– decompresses file.gz back to file.
7zr a -t7z <archive.7z> /folder/
– create 7z archive from a folder. You can also point to the file.
7zr a -tzip <archive.7z> /folder/
– create a zip archive from the folder. You can also point to the file.
7zr e files.7z
– extract 7z file.
unrar e file.rar </path>
– extract rar file to a specific path.
unrar x file.rar
– extract a rar file with their original directory structure.
rar a file.rar /folder/
– create rar file from a folder.
# Searching
grep <pattern> <files>
– Search for a pattern in files. -A NUM
, --after-context=NUM
– print NUM lines of trailing context after matching lines.-B NUM
, --before-context=NUM
– print NUM lines of leading context before matching lines.
grep -r <pattern> <dir>
– Search recursively for a pattern in dir.
<command> | grep <pattern>
– Search for patterns in the output of the command.
locate <file>
– find all instances of file.
find </path/> -iname pattern.extension
– Search for files with specified patterns and extensions in a specific path. -type f
– for files and -type d
– for directories.
find </path/> | grep 'word'
– Search for a word in filename in a specific path.
grep word /path/file
– Search for a word in the file.
Midnight Commander has the option to search for files and text.
Run mc
and hit Alt + Shfit + ?
to open a search window.
# Process Management
ps
– your active processes.
top
– all running processes.
htop
– console process manager, you need to install it. You will love it.
kill <pid>
– kill process id PID.
killall <name>
– kill all processes by name.
bg
– list/resume stopped or background jobs.
fg
– bring the most recent job to the foreground.
# Network
ping <host>
– ping host and output results.
whois <domain>
– get whois information for domain.
dig <domain>
– get DNS information for the domain.
dig -x <host>
– reverse lookup host.
wget <file>
– download file.
wget -c <file>
– continue a stopped download.
# SSH
ssh user@host -p 8022
– connect to host as a user on port 8022.
ssh-copy-id user@host
– add your key to the host for the user to enable a keyed or passwordless login.
# Installation
Install from source steps:
./configure
make
make install
Install from file:
dpkg - i <pkg.deb>
– install a package (Debian).
apt install <pkg.deb>
– install a package (Debian).
rpm - Uvh <pkg.rpm>
– install a package (RPM).
# Shortcuts
Ctrl+C
– halts the current command
Ctrl+Z
– stops the current command, resume with fg
in foreground or bg
in the background.
Ctrl+D
– log out of current session, similar to exit
.
Ctrl+W
– erases one word in the current line.
Ctrl+U
– erases the whole line.
Ctrl+R
– type to bring up a recent command.
!!
– repeats the last command.
&&
or ;
is used to chain commands together.
/
by itself at the end of a line is a means of concatenating lines together
|
– send the output of one command/program/process to another command/program/process for further processing.
exit
– log out of the current session.
# GitHub
git pull <http://some_repo/code.git /opt/code
– download/update local repository to the newest commit.
If you have a lot of local repos, like me, for example on your Kali machine and want to keep them all up to date you can use gitup. This tool is created for updating multiple git repositories at once. You can simply install it in Debian apt install gitup
. Then just run:
gitup /opt/
– updates all GitHub repositories located under /opt folder. You can do it folder by folder or recursively. You can also bookmark your repos gitup --add ~/repos
if you add many of them --add ~/repos/foo ~/repos/bar ~/repos/baz
you can just use gitup
command to update all of your bookmarks and not point to each folder.
# Python
Running python scripts is simple.
python script.py
– running Python script using default Python version.
python2.7 script.py
– running Python script using specified version.
To check your version just type python -V
.
Default Python in Kali Linux is set to 3, but some old, still good scripts are written in Python 2. Sometimes when I pull the script from GitHub and create a system alias for that script, to be recognized as a system command, it is executed with error, because of the default Python version. For example golismero.
Create system command based on script: ln -s ${PWD}/golismero.py /usr/bin/golismero
.
Now command golismero
is recognized by the system and I can run it whatever I am located.
The header of the Python file has defined shebang: #!/usr/bin/env python
so it will be executed as Python 3. You can change that to #!/usr/bin/env python2.7
and save, but you need to remember that every time you pull and update. To avoid this, create a shell script e.g.: golismero.sh
and add there:
1#!/bin/sh
2 python2.7 /opt/golismero/golismero.py
#!/bin/sh
python2.7 /opt/golismero/golismero.py
make it executable chmod u+x golismero.sh
and create link ln -s /opt/golismero.sh user/bin/golismero
.
You can also change the Python version globally:
sudo update-alternatives --config python
but I do not recommend this.
# Image conversion and optimization
Mass image file conversion:
mogrify -quality 80% *.jpg
– change the quality of all jpg files to 80%.
mogrify -format jpg *.png
– convert all png files to jpg files.
mogrify -format jpg -path ./new_folder *.png
– convert all png files to jpg files and save them in the new_folder.
mogrify -format jpg -resize 50% -path ./new_folder *.png
– convert all png files to jpg files, resize them by 50% and save them in new_folder.
mogrify -quality 85 -format jpg *.png && rm *.png
– convert all png files to jpg files located in the same folder, change jpg file quality to 85% and delete source png files after conversion.