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)
[pre class="brush:bash"]
# http://askubuntu.com/questions/645462/avconv-version-and-youtube-dl
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
[pre class="brush:bash"]
# http://askubuntu.com/questions/645462/avconv-version-and-youtube-dl
apt-cache search libav | grep libav
[/pre]
Execute modified command when called by adding alias to the bashrc file (source)
[pre class="brush:bash"]
# http://askubuntu.com/questions/645462/avconv-version-and-youtube-dl
echo "alias youtube-dl='youtube-dl --prefer-ffmpeg'" | 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 youtube-dl)
[/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)
[/pre]
# 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
[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]

[pre class="brush:bash"][/pre]





Comments

Popular posts from this blog

How to Safely Uninstall Python from Mac OS X

Mac Laptops comes with Python pre-installed, but if you install another version of Python probably from www.python.org it might interfere with the default Python installation. Thus it is recommended to install other Python versions through Homebrew or mac ports.

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.

What is your Lucky Colour?

Colours and You
Which colour can bring you luck and enhance your day? This can be identified by calculating the birth number,