Skip to main content

Basic Commands - Linux


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.

Identifying the IP address
[pre class="brush:bash"]
ip addr show
ifconfig
[/pre]
Changing the default password
[pre class="brush:bash"]
passwd
[/pre]
Display the amount of disk space available
[pre class="brush:bash"]
df -h # -h: human readable output format
[/pre]
scp copy
[pre class="brush:bash"]
scp /path/to/file username@a:/path/to/destination
scp username@b:/path/to/file /path/to/destination
[/pre]
Executing a command at a fixed interval using watch command
[pre class="brush:bash"]
watch -n x command
# "x" is the repeat time in seconds;"command" is the command that you want to execute
[/pre]
Running a command at a fixed interval
[pre class="brush:bash"]
while true; do
cmd >> output.txt
sleep 2
done

while true; do sleep 2; cmd >>output.txt; done &

[/pre]
Running a command in background
[pre class="brush:bash"]
nohup command &
[/pre]
Show the list of installed packages in Ubuntu or Debian
[pre class="brush:bash"]
dpkg --get-selections
[/pre]
Check the capacity of a directory
[pre class="brush:bash"]
du -sh /path/directory
# h: human readable output; s: total disk space used by the directory
[/pre]
Installing ffmpeg from ppa depository (add ppa to apt apt-get and install) - (source)
[pre class="brush:bash"]
sudo add-apt-repository ppa:mc3man/trusty-media
sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install ffmpeg
[/pre]
Get the version of the libav-tools (source)
[pre class="brush:bash"]
#
apt-cache search libav | grep libav
[/pre]
Execute modified command when called by adding alias to the bashrc file (source)
[pre class="brush:bash"]
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
[/pre]
Installing OpenVPN client and import *.ovpn (Windows) file to configure
[pre class="brush:bash"]
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.
[/pre]
Counting the number of files in a directory and sub-directories (source)
[pre class="brush:bash"]
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.
[/pre]
Extract a process detail from top command based on process name
[pre class="brush:bash"]
top -c -p $(pgrep -d',' -f ffmpeg)
[/pre]
Spitting a MP3 files into multiple files (source)
[pre class="brush:bash"]
ffmpeg -i 01.mp3 -f segment -segment_time 100 -c copy 01_%03d.mp3
[/pre]
Joining MP3 files to a single track:[pre class="brush:bash"]
cat *.mp3 > out.mp3
[/pre]
Copy/Sync a Directory on Local Computer
[pre class="brush:bash"]
rsync -avzh /root/rpmpkgs /tmp/backups/[/pre]
chmod
[pre class="brush:bash"]
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[/pre]
List all the directories with sizes
[pre class="brush:bash"]
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
[/pre]
List all the processes associated with a process-name
[pre class="brush:bash"]
ps -fC process-name
[/pre]
Deleting an entry from history 
[pre class="brush:bash"]
history -d line_number
[/pre]
List all the installed packages
[pre class="brush:bash"]
apt list --installed
[/pre]
Changing password of another user
[pre class="brush:bash"]
sudo passwd user_name
[/pre]
Terminal execute commands as another user
[pre class="brush:bash"]
sudo su user_name
[/pre]
Find UID of a user
[pre class="brush:bash"]
id -u user_name
[/pre]
Find GID of a user
[pre class="brush:bash"]
id -g user_name
[/pre]
Find all the groups a user belongs to
[pre class="brush:bash"]
id -G user_name
groups
groups username
[/pre]
Find all the groups associated with UID
[pre class="brush:bash"]
id user_name
[/pre]
Disable sudo password for a particular user
[pre class="brush:bash"]
sudo vim /etc/sudoers
#add the following to the above file
user_name ALL=(ALL) NOPASSWD:ALL
[/pre]
Renaming all files in a folder (Amend for eg: unix_)
[pre class="brush:bash"]
rename 's/^/Unix_/' *
[/pre]
List the number of threads for a process ID
[pre class="brush:bash"]
ps -o nlwp processID
cat /proc/processID/status | grep Threads
ls /proc/processID/task/ | wc -l
[/pre]
Setting time zone from terminal
[pre class="brush:bash"]sudo dpkg-reconfigure tzdata[/pre]
Syncing files using rsync
[pre class="brush:bash"]
rsync -aqv -e "ssh -T -o Compression=no -x" user@10.1.1.1:/source/path/* /destination/path/
rsync -aqvzh source/path destination/path --delete
[/pre]
Importing ssh key from github
[pre class="brush:bash"]ssh-import-id-gh user_id[/pre]
Changing default ssh port
[pre class="brush:bash"]
vi /etc/ssh/sshd_config
#locate the Port 22 line and change it
service sshd restart[/pre]
[pre class="brush:bash"][/pre]




Comments

Popular posts from this blog

Advantages and Disadvantages of Electro-oculography (EOG)

In the previous post we looked at what is EOG? In this post we will look at the advantages and disadvantages of EOG.

How to Convert Excel Charts into High Resolution Images

Microsoft Excel is widely used to generate graphs and to analyse data due to its simple graphical user interface. Have you ever tried to convert the charts into images? If yes, you might have used "Print Screen" feature to easily convert the charts into images. However most of the scientific journals require the images in high resolution TIFF format. So in this post we will look at how to convert Microsoft Excel Charts/Graphs easily into high resolution images.

Online Radio Stations

Some of the popular Tamil radio stations are listed here. You can listen to those radios here.