Basic Linux Command Line Tricks

Basic Linux Command Line Tricks

This tutorial will cover the basic Linux command tricks that can be used in terminal for your own uses.

To use the midnight command

The midnight command is used to run the commands graphically. Before using midnight commander you should install mc (midnight commander) package using yum command.

[root@linuxhelp Desktop]# yum install mc

To launch midnight commander

Execute the following command to launch the midnight commander.

[root@linuxhelp Desktop]# mc

To create directory in mc shell press mkdir option

In the above image you can see the options at the bottom of the mc shell.

To edit your file

To edit your file select that particular file and now click edit option.


To delete file or directory

To delete file or directory select that particular file or directory now click delete option.


To quit mc shell press quit option and press yes.

To display information about known users

To display information about known users in the system by using lslogins.

[root@linuxhelp Desktop]# lslogins
UID USER PWD-LOCK PWD-DENY LAST-LOGIN GECOS
0 root 0 0 03:08:39 root
1 bin 0 1 bin
2 daemon 0 1 daemon
3 adm 0 1 adm
4 lp 0 1 lp
5 sync 0 1 sync
6 shutdown 0 1 shutdown
.
.
.
.
497 dovenull 0 1 Dovecot' s unauthorized user
498 saslauth 0 1 Saslauthd user
499 rtkit 0 1 RealtimeKit
500 xguest 0 1 Guest
501 user1 0 0
65534 nfsnobody 0 1 Anonymous NFS User

To search only file in particular location

To search only file in particular location by using find command.

[root@linuxhelp ~]# find /root/Desktop -type f
/root/Desktop/file.txt
/root/Desktop/dir1/my.txt
/root/Desktop/my.txt
/root/Desktop/as
/root/Desktop/dir3/my.txt
.
.
.
.
/root/Desktop/dir2/my.txt
/root/Desktop/mydir
/root/Desktop/googlizer-0.1.tar.gz

To search file type with specific size

To search file type with specific size by using find command.

[root@linuxhelp Desktop]# find . -type f -size 1M
./dir1/my.txt
./dir3/my.txt
./googlizer/README
./googlizer/Makefile
.
.
.
./dir2/my.txt
./mydir
./googlizer-0.1.tar.gz

For more info about find command: https://www.linuxhelp.com/find-command/

To create directory tree

Use mkdir command with -p option to create directory tree.

[root@linuxhelp Desktop]# mkdir -p dir/a/b/c/d/e
[root@linuxhelp Desktop]# tree dir
dir
??? a
??? b
??? c
??? d
??? e

5 directories, 0 files

For more info about mkdir command: https://www.linuxhelp.com/mkdir-command/

Copy the single file into multiple directories

To copy the single file into multiple directories by use xargs command.

[root@linuxhelp Desktop]# echo /root/Desktop/dir1/ /root/Desktop/dir2/ /root/Desktop/dir3/ | xargs -n 1 cp /root/Desktop/my.txt
[root@linuxhelp Desktop]# cd dir1/
[root@linuxhelp dir1]# ls
my.txt
[root@linuxhelp Desktop]# cd dir2/
[root@linuxhelp dir2]# ls
my.txt
[root@linuxhelp Desktop]# cd dir3/
[root@linuxhelp dir3]# ls
my.txt

To remove the bytes or content from the file without deleting the file

Use > followed by the file name to delete only the content of the file.

[root@linuxhelp Desktop]# cat my.txt
qwerty
nokia
moto
sony
samsung
[root@linuxhelp Desktop]# >  my.txt
[root@linuxhelp Desktop]# cat my.txt

For more info about cat command: https://www.linuxhelp.com/cat-command/

To run the Previous Command

To run the command again, press the up key on your keyboard repeat. Instead the history command list all the command below.

[root@linuxhelp Desktop]# history
1 ip route show
2 ssh root@192.168.5.55
3 dig twitter.com
4 vim /etc/ssh/sshd_config
5 service sshd restart
6 ip a
7 vim /etc/ssh/sshd_config
8 iptables -F
9 vim /etc/sysconfig/network-scripts/ifcfg-eth0
.
.
.
100 ps -ef
101 ps -eo user,comm,pid
102 ps -rf | grep httpd
103 ps -ef | grep httpd
104 ps -ef | grep nfs
105 ps -ef | grep httpd
106 cd

If you want check particular previous command from history command shell, use below option here am going to execute number 6th command in history shell by using ‘ !’ factorial symbol.

[root@linuxhelp Desktop]# !6
ip a
1: lo: < LOOPBACK,UP,LOWER_UP>  mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: < BROADCAST,MULTICAST,UP,LOWER_UP>  mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:dd:23:8c brd ff:ff:ff:ff:ff:ff
inet 192.168.5.82/24 brd 192.168.5.255 scope global eth0
inet6 fe80::20c:29ff:fedd:238c/64 scope link
valid_lft forever preferred_lft forever
3: virbr0: < BROADCAST,MULTICAST,UP,LOWER_UP>  mtu 1500 qdisc noqueue state UNKNOWN
link/ether 52:54:00:86:7c:3d brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
4: virbr0-nic: < BROADCAST,MULTICAST>  mtu 1500 qdisc noop state DOWN qlen 500
link/ether 52:54:00:86:7c:3d brd ff:ff:ff:ff:ff:ff

To use shutdown command

If you want to shutdown your system with specified time duration, then follow the below command.

[root@linuxhelp Desktop]# shutdown 04:16

Now your system will be shutdown at time 04:16.

If you want to shut down your system after 15 minutes from your current time the follow below commands.

[root@linuxhelp Desktop]# shutdown +15

If you want to shut down your system immediately you should use below command.

[root@linuxhelp Desktop]# shutdown now

For more info about shutdown command: https://www.linuxhelp.com/shutdown-command/

FAQ
Q
What is the best alternative to find command?
A
Prefer using locate command for finding files.
Q
How can I install mc in fedora ?
A
Please use "dnf install mc" to install in Fedora.
Q
Is there any aleternate way other than "lslogins"
A
Prefer using "who" command "aureport" are some of the useful commands
Q
How to execute commands execyed in history directly?
A
Exclamatory mark followed by the command number in history status
Q
What time format is enabled for Shutdown command in Linux?
A
It is based on the Timezone assigned in it