Linux Basic Commands
Linux is a family of open-source free operating systems built using the Linux kernel. Linux is an incredibly powerful operating system when compared to other types of operating system for certain tasks, mostly command line based tasks. This means, it has a steep learning curve and you will be learning new things everyday. In this post I will be listing some of the basic commands that you might find useful to get started and to perform day to day activities in Linux.
Network
Identifying the IP address
ip addr show
ifconfig
Installing OpenVPN client and import *.ovpn (Windows) file to configure
sudo apt-get install openvpn
sudo apt-get install network-manager-openvpn-gnome
Now goto the network settings on the system tray -> “vpn connections” -> “configure vpn” -> “Add” -> select “Import saved VPN configuration” from the drop down list - > “create” -> select the *.ovpn file -> finally, type the username and password and click “ok”. Now go to the network settings and connect to the configured VPN server.
User management
Changing the default password
passwd
Find UID of a user
id -u user_name
Find GID of a user
id -g user_name
Find all the groups a user belongs to
id -G user_name
groups username
Find all the groups associated with UID
id user_name
Disable sudo password for a particular user
create a file in /etc/sudoers.d for example
sudo vim /etc/sudoers.d/dont_prompt_user
add the following to the above file
user_name ALL=(ALL) NOPASSWD:ALL
Bash
Enable debug
set -x
Disable debug
set +x
Deleting an entry from history
history -d line_number
Storage
Display the amount of disk space available
df -h # -h: human readable output format
Check the capacity of a directory
du -sh /path/directory
h: human readable output; s: total disk space used by the directory
scp copy
scp -r /path/to/file username@a:/path/to/destination
scp -r username@b:/path/to/file /path/to/destination
### Executing a command at a fixed interval using watch command
watch -n x command
# "x" is the repeat time in seconds;"command" is the command that you want to execute
System
Users connected to the system
who
Running a command at a fixed interval
while true; do
cmd >> output.txt
sleep 2
done
while true; do sleep 2; cmd >>output.txt; done &
Running a command in background
nohup command &
Show the list of installed packages in Ubuntu or Debian
dpkg --get-selections
Installing ffmpeg from ppa depository (add ppa to apt apt-get and install) - (source)
sudo add-apt-repository ppa:mc3man/trusty-media
sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install ffmpeg
Get the version of the libav-tools (source)
apt-cache search libav | grep libav
Execute modified command when called by adding alias to the bashrc file (source)
echo "alias ls='ls -alh'" | tee -a ~/.bashrc; . ~/.bashrc
# alternatively, you can edit this file by opening this file from your home directory
vim .bashrc
Counting the number of files in a directory and sub-directories (source)
find . -type f | wc -l
# . - this directory and all subdirectories
# -type f - find all files
# | - piped into wc - word count and the -l - tells to count the lines from wc
# or
find . | wc -l
# this counts the files and directories within the current directory.
Extract a process detail from top command based on process name
top -c -p $(pgrep -d',' -f ffmpeg)
Spitting a MP3 files into multiple files (source)
ffmpeg -i 01.mp3 -f segment -segment_time 100 -c copy 01_%03d.mp3
Joining MP3 files to a single track:[pre class=”brush:bash”]
cat *.mp3 > out.mp3
rsync
Copy/Sync a Directory on Local Computer
rsync -avzh /root/rpmpkgs/folder/ /tmp/backups/folder/ --delete
Syncing files using rsync across network
rsync -av -e "ssh -T -o Compression=no -x" user@10.1.1.1:/source/path/ /destination/path/
rsync -avzhe "ssh -i ~/.ssh/id_rsa" user@10.1.1.1:/source/path/ /destination/path/
chmod
chmod 700 myDir #7-user; 0:Group; 0: Others - read(4), write(2), execute(1)
# Permission rwx Binary
#7 read, write and execute rwx 111
#6 read and write rw- 110
#5 read and execute r-x 101
#4 read only r-- 100
#3 write and execute -wx 011
#2 write only -w- 010
#1 execute only --x 001
#0 none --- 000
| No | Permission | rwx | Binary | |——|——|——|——| | 7 | read, write and execute | rwx | 111 | | 6 | read and write | rw- | 110 | | 5 | read and execute | r-x | 101 | | 4 | read only | r– | 100 | | 3 | write and execute | -wx | 011 | | 2 | write only | -w- | 010 | | 1 | execute only | –x | 001 | | 0 | none | — | 000 |
List all the directories with sizes
du -sh *
du: disk usage
# -s: Display an entry for each specified file
# -h: human readable format
du -sh * | sort -n # sort folders by size
du -sh * | tail -r # sort by largest first
List all the processes associated with a process-name
ps -fC process-name
List all the installed packages
apt list --installed
Changing password of another user
sudo passwd user_name
Terminal execute commands as another user
sudo su user_name
Renaming all files in a folder (Amend for eg: unix_)
rename 's/^/Unix_/' *
List the number of threads for a process ID
ps -o nlwp processID
cat /proc/processID/status | grep Threads
ls /proc/processID/task/ | wc -l
Setting time zone from terminal
sudo dpkg-reconfigure tzdata
Importing ssh key from github
ssh-import-id-gh user_id
Changing default ssh port
vi /etc/ssh/sshd_config
#locate the Port 22 line and change it
service sshd restart
Compressing PDF files using ghostscript
sudo apt install ghostscript
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -dNOPAUSE -dQUIET -dBATCH -sOutputFile=compressed_PDF_file.pdf input_PDF_file.pdf
# some example dPDSETTINS: /prepress /printer /ebook /screen
Ubuntu .deb package installation/uninstallation
installing
sudo dpkg -i package_file.deb
list the packages installed with name urserver
sudo dpkg -l '*urserver*'
remove the package itself (without the configuration files
sudo dpkg -r urserver
delete (purge) the package completely (with configuration files)
sudo dpkg -P urserver
check if the package has been removed successfully
sudo dpkg -l urserver
Check CPU microarchitecture
cat /sys/devices/cpu/caps/pmu_name
OS Details
OS release - works with atest ubuntu
cat /etc/os-release
another way to check os details
lsb_release -a
Find command
sudo find / -name "name_of_the_file"
add a user to sudo
usermod -aG sudo username
Comments