Frequently Used Unix Commands for Software Tester
Unix commands can be very handy during any query result fetching from server log, here are some useful Unix commands for testers.
Photo by Gabriel Heinzer on Unsplash
As a software tester, during backend testing you need the skill set to monitor server logs, finding culprits by analyzing logs, know the processes, network etc. Some unix commands can be very helpful in this regards. So let's get started to explore some useful commands.
date
date command is to view the system time and with different query parameter view the date time like which day was on 1995-04-28, date time of 2 hours ago etc.
root@K-ASHIK-L4:~/linux-command-practice/cat# date
Sun Jun 12 15:53:18 +06 2022
root@K-ASHIK-L4:~/linux-command-practice/cat# date -d "1995-04-28"
Fri Apr 28 00:00:00 +06 1995
root@K-ASHIK-L4:~/linux-command-practice/cat# date --date='2 years ago'
Fri Jun 12 15:54:48 +06 2020
root@K-ASHIK-L4:~/linux-command-practice/cat# date --date='1 month ago'
Thu May 12 15:56:06 +06 2022
uptime
uptime command lets you know the current system load and it's up time.
root@K-ASHIK-L4:~/linux-command-practice/cat# uptime
15:57:39 up 5:02, 0 users, load average: 0.00, 0.00, 0.00
cal
- View calendar of any year
- View calendar of any month of a year: cal MM YYYY
su
su (Switch User) command is used to switch between users:
root@K-ASHIK-L4:~# su - humayun-ashik
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
humayun-ashik@K-ASHIK-L4:~$ su - root
Password:
root@K-ASHIK-L4:~#
ps
ps command is used for viewing information related with the processes on a system which stands as abbreviation for “Process Status”.
- View the processes for the current shell:
root@K-ASHIK-L4:~# ps
PID TTY TIME CMD
79 pts/0 00:00:00 su
80 pts/0 00:00:03 bash
1358 pts/0 00:00:00 su
1374 pts/0 00:00:00 su
1375 pts/0 00:00:00 bash
1406 pts/0 00:00:00 ps
- View all the running processes
root@K-ASHIK-L4:~# ps -e
PID TTY TIME CMD
1 ? 00:00:00 init
7 ? 00:00:00 init
8 ? 00:00:02 init
9 pts/0 00:00:00 bash
79 pts/0 00:00:00 su
80 pts/0 00:00:03 bash
1358 pts/0 00:00:00 su
1359 pts/0 00:00:00 bash
1374 pts/0 00:00:00 su
1375 pts/0 00:00:00 bash
1407 pts/0 00:00:00 ps
- View all processes running by you:
root@K-ASHIK-L4:~# ps -x
PID TTY STAT TIME COMMAND
1 ? Sl 0:00 /init
7 ? Ss 0:00 /init
8 ? R 0:02 /init
79 pts/0 S 0:00 su - root
80 pts/0 S 0:03 -bash
1358 pts/0 S 0:00 su - humayun-ashik
1374 pts/0 S 0:00 su - root
1375 pts/0 S 0:00 -bash
1409 pts/0 R+ 0:00 ps -x
- View full format listing using -f option:
root@K-ASHIK-L4:~# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 10:55 ? 00:00:00 /init
root 7 1 0 10:55 ? 00:00:00 /init
root 8 7 0 10:55 ? 00:00:02 /init
humayun+ 9 8 0 10:55 pts/0 00:00:00 -bash
root 79 9 0 10:58 pts/0 00:00:00 su - root
root 80 79 0 10:58 pts/0 00:00:03 -bash
root 1358 80 0 17:06 pts/0 00:00:00 su - humayun-ashik
humayun+ 1359 1358 0 17:06 pts/0 00:00:00 -bash
root 1374 1359 0 17:07 pts/0 00:00:00 su - root
root 1375 1374 0 17:07 pts/0 00:00:00 -bash
root 1410 1375 0 17:23 pts/0 00:00:00 ps -ef
kill
kill command is used to manually terminate a process. kill command sends a signal to a process which terminates the process
- View all the signals:
root@K-ASHIK-L4:~# kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
- kill any process using pid:
root@K-ASHIK-L4:~# ps
PID TTY TIME CMD
79 pts/0 00:00:00 su
80 pts/0 00:00:03 bash
1358 pts/0 00:00:00 su
1374 pts/0 00:00:00 su
1375 pts/0 00:00:00 bash
1413 pts/0 00:00:00 ps
root@K-ASHIK-L4:~#
root@K-ASHIK-L4:~# kill 1413
- For forceful kill:
root@K-ASHIK-L4:~# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 10:55 ? 00:00:00 /init
root 7 1 0 10:55 ? 00:00:00 /init
root 8 7 0 10:55 ? 00:00:02 /init
humayun+ 9 8 0 10:55 pts/0 00:00:00 -bash
root 79 9 0 10:58 pts/0 00:00:00 su - root
root 80 79 0 10:58 pts/0 00:00:03 -bash
root 1358 80 0 17:06 pts/0 00:00:00 su - humayun-ashik
humayun+ 1359 1358 0 17:06 pts/0 00:00:00 -bash
root 1374 1359 0 17:07 pts/0 00:00:00 su - root
root 1375 1374 0 17:07 pts/0 00:00:00 -bash
root 1415 1375 0 17:35 pts/0 00:00:00 ps -ef
root@K-ASHIK-L4:~#
root@K-ASHIK-L4:~# kill -9 1374
root@K-ASHIK-L4:~# Killed
ping
-ping command is used to know the connection status from your machine to any destination server
root@K-ASHIK-L4:~# ping google.com
PING google.com (74.125.24.102) 56(84) bytes of data.
From 192.168.43.1 (192.168.43.1) icmp_seq=16 Destination Net Unreachable
^C
--- google.com ping statistics ---
25 packets transmitted, 0 received, +1 errors, 100% packet loss, time 25034ms
pipe 2
free
free command provides information about the total amount of the physical and swap memory, as well as the free and used memory and free memories.
root@K-ASHIK-L4:~/linux-command-practice# free
total used free shared buff/cache available
Mem: 5022200 87728 4758416 72 176056 4726356
Swap: 2097152 0 2097152
root@K-ASHIK-L4:~/linux-command-practice#
root@K-ASHIK-L4:~/linux-command-practice# free -h
total used free shared buff/cache available
Mem: 4.8Gi 85Mi 4.5Gi 0.0Ki 171Mi 4.5Gi
Swap: 2.0Gi 0B 2.0Gi
du
du (Disk Usage) is a standard Unix/Linux command, used to check the information of disk usage of files and directories.
root@K-ASHIK-L4:~/linux-command-practice# du -h grep/
8.0K grep/grepfolder
16K grep/
df
df stands for "disk filesystem" and is used to display the disk space used in the file system.
root@K-ASHIK-L4:~/linux-command-practice# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sdb 251G 1.8G 237G 1% /
none 2.4G 4.0K 2.4G 1% /mnt/wsl
tools 223G 111G 112G 50% /init
none 2.4G 0 2.4G 0% /dev
none 2.4G 8.0K 2.4G 1% /run
none 2.4G 0 2.4G 0% /run/lock
none 2.4G 0 2.4G 0% /run/shm
none 2.4G 0 2.4G 0% /run/user
tmpfs 2.4G 0 2.4G 0% /sys/fs/cgroup
drivers 223G 111G 112G 50% /usr/lib/wsl/drivers
lib 223G 111G 112G 50% /usr/lib/wsl/lib
drvfs 223G 111G 112G 50% /mnt/c
drvfs 366G 28G 339G 8% /mnt/d
drvfs 366G 20G 347G 6% /mnt/e
pwd
pwd stands for Print Working Directory. This command is contained in /usr/bin/pwd. It is frequently used to know the current directory location.
root@K-ASHIK-L4:/usr/bin# pwd
/usr/bin
clear
To clear the current terminal text, clear command is used. Just write clear and hit enter to clear the terminal text.
mkdir
To create directories mkdir is used.
root@K-ASHIK-L4:~/linux-command-practice# mkdir dir1
root@K-ASHIK-L4:~/linux-command-practice#
root@K-ASHIK-L4:~/linux-command-practice# ls -l
total 4
drwxr-xr-x 2 root root 4096 Jun 12 11:31 dir1
It can be also used to create multiple directories at a time:
root@K-ASHIK-L4:~/linux-command-practice# mkdir dir2 dir3 dir4
root@K-ASHIK-L4:~/linux-command-practice#
root@K-ASHIK-L4:~/linux-command-practice# ls -l
total 16
drwxr-xr-x 2 root root 4096 Jun 12 11:31 dir1
drwxr-xr-x 2 root root 4096 Jun 12 11:34 dir2
drwxr-xr-x 2 root root 4096 Jun 12 11:34 dir3
drwxr-xr-x 2 root root 4096 Jun 12 11:34 dir4
cd
cd command is used for changing working directory.
root@K-ASHIK-L4:~/linux-command-practice# pwd
/root/linux-command-practice
root@K-ASHIK-L4:~/linux-command-practice#
root@K-ASHIK-L4:~/linux-command-practice# cd dir1
root@K-ASHIK-L4:~/linux-command-practice/dir1#
root@K-ASHIK-L4:~/linux-command-practice/dir1#
root@K-ASHIK-L4:~/linux-command-practice/dir1# pwd
/root/linux-command-practice/dir1
Here before changing, working directory was /root/linux-command-practice and after changing working directory is /root/linux-command-practice/dir1
To go back to previous directory we can use cd ..
root@K-ASHIK-L4:~/linux-command-practice/dir1# pwd
/root/linux-command-practice/dir1
root@K-ASHIK-L4:~/linux-command-practice/dir1# cd ..
root@K-ASHIK-L4:~/linux-command-practice# pwd
/root/linux-command-practice
We can also go to any directory by mentioning it's path:
root@K-ASHIK-L4:~/linux-command-practice# ls
dir1 dir2 dir3 dir4
root@K-ASHIK-L4:~/linux-command-practice# cd dir2
root@K-ASHIK-L4:~/linux-command-practice/dir2#
root@K-ASHIK-L4:~/linux-command-practice/dir2# cd ../dir4
root@K-ASHIK-L4:~/linux-command-practice/dir4#
Here 4 directories in working directory - dir1 dir2 dir3 dir4. Firstly we move to dir2 directory and then from dir2 go to dir4.
touch
touch command is used for creating file(s) without content.
root@K-ASHIK-L4:~/linux-command-practice/touch# ls -l
total 0
root@K-ASHIK-L4:~/linux-command-practice/touch# touch file1.txt
root@K-ASHIK-L4:~/linux-command-practice/touch#
root@K-ASHIK-L4:~/linux-command-practice/touch# ls -l
total 0
-rw-r--r-- 1 root root 0 Jun 12 11:58 file1.txt
We can use multiple empty files in a single shot:
root@K-ASHIK-L4:~/linux-command-practice/touch# touch file2.txt logfile.log csvfile.csv
root@K-ASHIK-L4:~/linux-command-practice/touch#
root@K-ASHIK-L4:~/linux-command-practice/touch# ls -l
total 0
-rw-r--r-- 1 root root 0 Jun 12 12:00 csvfile.csv
-rw-r--r-- 1 root root 0 Jun 12 11:58 file1.txt
-rw-r--r-- 1 root root 0 Jun 12 12:00 file2.txt
-rw-r--r-- 1 root root 0 Jun 12 12:00 logfile.log
We can create file with any timestamp by following: touch -t YYYYMMDDHHMM fileName
root@K-ASHIK-L4:~/linux-command-practice/touch# touch -t 202105111212 time.txt
root@K-ASHIK-L4:~/linux-command-practice/touch#
root@K-ASHIK-L4:~/linux-command-practice/touch# ls -l
total 0
-rw-r--r-- 1 root root 0 Jun 12 12:00 csvfile.csv
-rw-r--r-- 1 root root 0 Jun 12 11:58 file1.txt
-rw-r--r-- 1 root root 0 Jun 12 12:00 file2.txt
-rw-r--r-- 1 root root 0 Jun 12 12:00 logfile.log
-rw-r--r-- 1 root root 0 May 11 2021 time.txt
cat
cat (concatenate) command is very frequently used command. It reads the contents of files and display as outputs.
Some of usages of cat command listed below:
- Creating files with some content: Use Ctrl + D to exit after finishing content add.
root@K-ASHIK-L4:~/linux-command-practice/cat# cat > filewithcontent.txt
line1
line2
line3
line4
- View the contents of a file:
root@K-ASHIK-L4:~/linux-command-practice/cat# cat filewithcontent.txt
line1
line2
line3
line4
root@K-ASHIK-L4:~/linux-command-practice/cat#
- View contents from multiple files: Concatenates the files content sequentially and displays the output.
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# cat filewithcontent.txt anotherfile.log
line1
line2
line3
line4
anotherfile-line-1
anotherfile-line-2
anotherfile-line-3
anotherfile-line-4
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# cat anotherfile.log filewithcontent.txt
anotherfile-line-1
anotherfile-line-2
anotherfile-line-3
anotherfile-line-4
line1
line2
line3
line4
root@K-ASHIK-L4:~/linux-command-practice/cat#
- Display line numbers in content:
root@K-ASHIK-L4:~/linux-command-practice/cat# cat -n filewithcontent.txt
1 line1
2 line2
3 line3
4 line4
root@K-ASHIK-L4:~/linux-command-practice/cat#
- Replace one file's content with another: cat anotherfile.log > filewithcontent.txt Here content of file filewithcontent.txt replaced with content of anotherfile.log file.
root@K-ASHIK-L4:~/linux-command-practice/cat# cat filewithcontent.txt
line1
line2
line3
line4
root@K-ASHIK-L4:~/linux-command-practice/cat# cat anotherfile.log
anotherfile-line-1
anotherfile-line-2
anotherfile-line-3
anotherfile-line-4
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# cat anotherfile.log > filewithcontent.txt
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# cat filewithcontent.txt
anotherfile-line-1
anotherfile-line-2
anotherfile-line-3
anotherfile-line-4
root@K-ASHIK-L4:~/linux-command-practice/cat#
- Append standard output of one file at the end of another file: cat filewithcontent.txt >> outputfile.log
root@K-ASHIK-L4:~/linux-command-practice/cat# cat outputfile.log
This is output file, you can append here
>
root@K-ASHIK-L4:~/linux-command-practice/cat# cat filewithcontent.txt >> outputfile.log
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# cat outputfile.log
This is output file, you can append here
>
anotherfile-line-1
anotherfile-line-2
anotherfile-line-3
anotherfile-line-4
root@K-ASHIK-L4:~/linux-command-practice/cat#
- Append contents of multiples files and insert all output into a new file:
root@K-ASHIK-L4:~/linux-command-practice/cat# ls
logfile.log outputfile.log test1.log test2.txt
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# cat test1.log
line-1
line-2
line-3
line-4
root@K-ASHIK-L4:~/linux-command-practice/cat# cat test2.txt
anotherfile-line-1
anotherfile-line-2
anotherfile-line-3
anotherfile-line-4
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# cat test1.log test2.txt > test3.log
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# ls
logfile.log outputfile.log test1.log test2.txt test3.log
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# cat test3.log
line-1
line-2
line-3
line-4
anotherfile-line-1
anotherfile-line-2
anotherfile-line-3
anotherfile-line-4
root@K-ASHIK-L4:~/linux-command-practice/cat#
- Create a hidden file:
root@K-ASHIK-L4:~/linux-command-practice/cat# cat > .hiddenfile.txt
This is a hidden file
root@K-ASHIK-L4:~/linux-command-practice/cat#
mv
mv command is used to rename a file.
root@K-ASHIK-L4:~/linux-command-practice# ls
cat dir1 dir100 dir2 dir3 grep touch
root@K-ASHIK-L4:~/linux-command-practice#
root@K-ASHIK-L4:~/linux-command-practice# mv dir100 mv-command
root@K-ASHIK-L4:~/linux-command-practice#
root@K-ASHIK-L4:~/linux-command-practice# ls
cat dir1 dir2 dir3 grep mv-command touch
root@K-ASHIK-L4:~/linux-command-practice#
rm
rm command is used to delete the files.
- Ask permission to delete:
root@K-ASHIK-L4:~/linux-command-practice/cat# rm -i test2.txt
rm: remove regular file 'test2.txt'? y
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# ls
catdir logfile.log.gz numbers.txt outputfile.log test1.log test3.log
- Force delete: no permission asked
root@K-ASHIK-L4:~/linux-command-practice/cat# ls
catdir logfile.log.gz numbers.txt outputfile.log test1.log test3.log
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# rm -f test1.log
root@K-ASHIK-L4:~/linux-command-practice/cat# ls
catdir logfile.log.gz numbers.txt outputfile.log test3.log
- Delete all the files with extension .log:
root@K-ASHIK-L4:~/linux-command-practice/cat# ls
catdir logfile.log.gz numbers.txt outputfile.log test3.log
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# rm *log
root@K-ASHIK-L4:~/linux-command-practice/cat# ls
catdir logfile.log.gz numbers.txt
root@K-ASHIK-L4:~/linux-command-practice/cat#
- Delete recursively files and directories:
root@K-ASHIK-L4:~/linux-command-practice/cat# ls
catdir logfile.log.gz numbers.txt willbedeleted
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# rm -r *
root@K-ASHIK-L4:~/linux-command-practice/cat# ls
rmdir
rmdir commnd is used to delete empty directories and thros an error if directory is not empty.
root@K-ASHIK-L4:~# rmdir linux-command-practice
rmdir: failed to remove 'linux-command-practice': Directory not empty
root@K-ASHIK-L4:~/linux-command-practice/rmdir# ls
rmdir1 rmdir2
root@K-ASHIK-L4:~/linux-command-practice/rmdir# rmdir -v rmdir1
rmdir: removing directory, 'rmdir1'
Here -v option is used to get the verbose when deleting directories.
- Remove all directory:
root@K-ASHIK-L4:~/linux-command-practice/rmdir# ls
rmdir2 rmdir3 rmdir4 rmdir5 rmdir6 rmdir7
root@K-ASHIK-L4:~/linux-command-practice/rmdir# rmdir -v *
rmdir: removing directory, 'rmdir2'
rmdir: removing directory, 'rmdir3'
rmdir: removing directory, 'rmdir4'
rmdir: removing directory, 'rmdir5'
rmdir: removing directory, 'rmdir6'
rmdir: removing directory, 'rmdir7'
cp
cp command is used to copy files from one source to destination.
root@K-ASHIK-L4:~/linux-command-practice/touch# ls
csvfile.csv file1.txt file2.txt logfile.log time.txt
root@K-ASHIK-L4:~/linux-command-practice/touch# cp /root/linux-command-practice/touch/*.txt /root/linux-command-practice/cat/
root@K-ASHIK-L4:~/linux-command-practice/touch# ls -l ../cat/
total 0
-rw-r--r-- 1 root root 0 Jun 12 16:53 file1.txt
-rw-r--r-- 1 root root 0 Jun 12 16:53 file2.txt
-rw-r--r-- 1 root root 0 Jun 12 16:53 time.txt
- Using cp -r option we can copy directory with all contents:
root@K-ASHIK-L4:~/linux-command-practice# cp -r grep dir1
root@K-ASHIK-L4:~/linux-command-practice# cd dir1
root@K-ASHIK-L4:~/linux-command-practice/dir1# ls
grep
root@K-ASHIK-L4:~/linux-command-practice/dir1# cd grep/
root@K-ASHIK-L4:~/linux-command-practice/dir1/grep# ls
grepPracticeLog.log grepfolder
ls
- List files and directories:
root@K-ASHIK-L4:~/linux-command-practice/cat# ls
catdir logfile.log outputfile.log test1.log test2.txt test3.log
- List files or directories with their sizes:
root@K-ASHIK-L4:~/linux-command-practice/cat# ls -s
total 24
4 catdir 4 logfile.log 4 outputfile.log 4 test1.log 4 test2.txt 4 test3.log
- List files in long format:
root@K-ASHIK-L4:~/linux-command-practice/cat# ls -l
total 24
drwxr-xr-x 2 root root 4096 Jun 12 13:00 catdir
-rw-r--r-- 1 root root 1184 Jun 12 12:26 logfile.log
-rw-r--r-- 1 root root 122 Jun 12 12:45 outputfile.log
-rw-r--r-- 1 root root 28 Jun 12 12:49 test1.log
-rw-r--r-- 1 root root 76 Jun 12 12:31 test2.txt
-rw-r--r-- 1 root root 104 Jun 12 12:50 test3.log
Here 1st column represents content permission, 2nd column represents no. of links , 3rd column owner, 4th group owner 5th size in bytes, 6th date modified and 7th is the file/directory name.
- List files including hidden files:
root@K-ASHIK-L4:~/linux-command-practice/cat# ls -al
total 36
drwxr-xr-x 3 root root 4096 Jun 12 13:00 .
drwxr-xr-x 8 root root 4096 Jun 12 12:16 ..
-rw-r--r-- 1 root root 22 Jun 12 12:57 .hiddenfile.txt
drwxr-xr-x 2 root root 4096 Jun 12 13:00 catdir
-rw-r--r-- 1 root root 1184 Jun 12 12:26 logfile.log
-rw-r--r-- 1 root root 122 Jun 12 12:45 outputfile.log
-rw-r--r-- 1 root root 28 Jun 12 12:49 test1.log
-rw-r--r-- 1 root root 76 Jun 12 12:31 test2.txt
-rw-r--r-- 1 root root 104 Jun 12 12:50 test3.log
- List files sort by size descending:
root@K-ASHIK-L4:~/linux-command-practice/cat# ls -lS
total 24
drwxr-xr-x 2 root root 4096 Jun 12 13:00 catdir
-rw-r--r-- 1 root root 1184 Jun 12 12:26 logfile.log
-rw-r--r-- 1 root root 122 Jun 12 12:45 outputfile.log
-rw-r--r-- 1 root root 104 Jun 12 12:50 test3.log
-rw-r--r-- 1 root root 76 Jun 12 12:31 test2.txt
-rw-r--r-- 1 root root 28 Jun 12 12:49 test1.log
- List files sort by size ascending:
root@K-ASHIK-L4:~/linux-command-practice/cat# ls -lrS
total 24
-rw-r--r-- 1 root root 28 Jun 12 12:49 test1.log
-rw-r--r-- 1 root root 76 Jun 12 12:31 test2.txt
-rw-r--r-- 1 root root 104 Jun 12 12:50 test3.log
-rw-r--r-- 1 root root 122 Jun 12 12:45 outputfile.log
-rw-r--r-- 1 root root 1184 Jun 12 12:26 logfile.log
drwxr-xr-x 2 root root 4096 Jun 12 13:00 catdir
- List files sort by date-time descending:
root@K-ASHIK-L4:~/linux-command-practice/cat# ls -lt
total 24
drwxr-xr-x 2 root root 4096 Jun 12 13:00 catdir
-rw-r--r-- 1 root root 104 Jun 12 12:50 test3.log
-rw-r--r-- 1 root root 28 Jun 12 12:49 test1.log
-rw-r--r-- 1 root root 122 Jun 12 12:45 outputfile.log
-rw-r--r-- 1 root root 76 Jun 12 12:31 test2.txt
-rw-r--r-- 1 root root 1184 Jun 12 12:26 logfile.log
- List files sort by date-time ascending
root@K-ASHIK-L4:~/linux-command-practice/cat# ls -lrt
total 24
-rw-r--r-- 1 root root 1184 Jun 12 12:26 logfile.log
-rw-r--r-- 1 root root 76 Jun 12 12:31 test2.txt
-rw-r--r-- 1 root root 122 Jun 12 12:45 outputfile.log
-rw-r--r-- 1 root root 28 Jun 12 12:49 test1.log
-rw-r--r-- 1 root root 104 Jun 12 12:50 test3.log
drwxr-xr-x 2 root root 4096 Jun 12 13:00 catdir
- List files in readable size format:
root@K-ASHIK-L4:~/linux-command-practice/cat# ls -lh
total 24K
drwxr-xr-x 2 root root 4.0K Jun 12 13:00 catdir
-rw-r--r-- 1 root root 1.2K Jun 12 12:26 logfile.log
-rw-r--r-- 1 root root 122 Jun 12 12:45 outputfile.log
-rw-r--r-- 1 root root 28 Jun 12 12:49 test1.log
-rw-r--r-- 1 root root 76 Jun 12 12:31 test2.txt
-rw-r--r-- 1 root root 104 Jun 12 12:50 test3.log
- List all the files with extension .log in a readable size format and ascending of timestamp:
root@K-ASHIK-L4:~/linux-command-practice/cat# ls -lrth *.log
-rw-r--r-- 1 root root 1.2K Jun 12 12:26 logfile.log
-rw-r--r-- 1 root root 122 Jun 12 12:45 outputfile.log
-rw-r--r-- 1 root root 28 Jun 12 12:49 test1.log
-rw-r--r-- 1 root root 104 Jun 12 12:50 test3.log
wc
wc command stands for word count. It is used to find out number of lines, word count, byte and characters count in the files specified.
- Print the no of lines in a file:
root@K-ASHIK-L4:~/linux-command-practice/cat# wc -l logfile.log
37 logfile.log
root@K-ASHIK-L4:~/linux-command-practice/cat#
- Print the no of words in a file:
root@K-ASHIK-L4:~/linux-command-practice/cat# cat test1.log
line-1 colum2
line-2 column2
line-3 column2
line-4 column2
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# wc -w test1.log
8 test1.log
- Display the count of characters:
root@K-ASHIK-L4:~/linux-command-practice/cat# wc -c test1.log
59 test1.log
uniq
uniq command is used to get the unique or duplicate lines from a file.
-d Display duplicate lines -u Display uniq lines -c Counts number of times each word has occurred in a file
root@K-ASHIK-L4:~/linux-command-practice/cat# cat test1.log
line-1 colum2
line-2 column2
line-3 column2
line-4 column2
line-4 column2
line-4 column2
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# uniq -c test1.log
1 line-1 colum2
1 line-2 column2
1 line-3 column2
3 line-4 column2
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# uniq -d test1.log
line-4 column2
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# uniq -u test1.log
line-1 colum2
line-2 column2
line-3 column2
root@K-ASHIK-L4:~/linux-command-practice/cat#
head
head command is used to view the first n lines of content.
root@K-ASHIK-L4:~/linux-command-practice/cat# head -3 logfile.log
"201000225","00000000",10100066
"201000228","00000000",10100069
"202000231","00000000",10200072
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# head -5 logfile.log
"201000225","00000000",10100066
"201000228","00000000",10100069
"202000231","00000000",10200072
"202000234","00000000",10200075
"203000240","00000000",10300078
tail
tail command is used to view the last n lines of content.
root@K-ASHIK-L4:~/linux-command-practice/cat# tail -2 logfile.log
"218000382","00000000",11800171
"218000383","00000000",11800174
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# tail -5 logfile.log
"216000369","00000000",11600162
"217000375","00000000",11700165
"217000378","00000000",11700168
"218000382","00000000",11800171
"218000383","00000000",11800174
root@K-ASHIK-L4:~/linux-command-practice/cat#
- Print lines 26-30 using head and tail:
root@K-ASHIK-L4:~/linux-command-practice/cat# cat logfile.log | head -30 | tail -5
"213000339","00000000",11300141
"213000342","00000000",11300144
"214000348","00000000",11400147
"214000351","00000000",11400150
"215000357","00000000",11500153
cut
cut command is used to cut the required text from a file. It can cut the data on the columns and fields.
root@K-ASHIK-L4:~/linux-command-practice/cat# cat test1.log
line-1 colum2
line-2 column2
line-3 column2
line-4 column2
line-4 column2
line-4 column2
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# cut -c5 test1.log
-
-
-
-
-
-
root@K-ASHIK-L4:~/linux-command-practice/cat#
root@K-ASHIK-L4:~/linux-command-practice/cat# cut -f2 test1.log
colum2
column2
column2
column2
column2
column2
root@K-ASHIK-L4:~/linux-command-practice/cat#
sort
- Sort numbers
root@K-ASHIK-L4:~/linux-command-practice/cat# cat numbers.txt 2 4 1 54 32 -4 9 100 root@K-ASHIK-L4:~/linux-command-practice/cat# root@K-ASHIK-L4:~/linux-command-practice/cat# root@K-ASHIK-L4:~/linux-command-practice/cat# sort -n numbers.txt -4 1 2 4 9 32 54 100
Sort text files
root@K-ASHIK-L4:~/linux-command-practice/cat# cat test3.log line-1 line-2 line-3 line-4 anotherfile-line-1 anotherfile-line-2 anotherfile-line-3 anotherfile-line-4 root@K-ASHIK-L4:~/linux-command-practice/cat# root@K-ASHIK-L4:~/linux-command-practice/cat# root@K-ASHIK-L4:~/linux-command-practice/cat# sort test3.log anotherfile-line-1 anotherfile-line-2 anotherfile-line-3 anotherfile-line-4 line-1 line-2 line-3 line-4 root@K-ASHIK-L4:~/linux-command-practice/cat#
Sort column 3 values in reverse order:
root@K-ASHIK-L4:~/linux-command-practice/cat# sort -rk 3n logfile.log "218000383","00000000",11800174 "218000382","00000000",11800171 "217000378","00000000",11700168 "217000375","00000000",11700165
grep
grep command is used for searching a required pattern in a file.
- case insensitive search
root@K-ASHIK-L4:~/linux-command-practice/grep# grep -i "jmeter" grepPracticeLog.log
2. Download apache Jmeter latest version and extract
6. Then place extracted jar file into your jmeter /lib/ext directory.
7. Start JMETER and write API test script for following two cases: Here email must be generated from your jar file which you have performed in step 6 and 7.
root@K-ASHIK-L4:~/linux-command-practice/grep#
Here -i option will search log file for the string inside the "" case insensitively.
- Count the no of occurrences
root@K-ASHIK-L4:~/linux-command-practice/grep# grep -c "jmeter" grepPracticeLog.log
1
root@K-ASHIK-L4:~/linux-command-practice/grep# grep -ic "jmeter" grepPracticeLog.log
3
- Search with whole word match:
root@K-ASHIK-L4:~/linux-command-practice/grep# grep -w "JMETER" grepPracticeLog.log
7. Start JMETER and write API test script for following two cases: Here email must be generated from your jar file which you have performed in step 6 and 7.
- Display only the matched pattern:
root@K-ASHIK-L4:~/linux-command-practice/grep# cat grepPracticeLog.log
1. Setup Intellij IDEA
2. Download apache Jmeter latest version and extract
3. Create a java project in Intellij IDEA
4. Write a class GenerateRandomEmail and inside this class create a method getRandomEmail() which should return any random valid email.
5. Make a jar file from Intellij IDEA of your written project which you have performed in step 3 and 4.
6. Then place extracted jar file into your jmeter /lib/ext directory.
7. Start JMETER and write API test script for following two cases: Here email must be generated from your jar file which you have performed in step 6 and 7.
Case - 1: User registration with valid email.
Case - 2: User registration with already registered email.
root@K-ASHIK-L4:~/linux-command-practice/grep#
root@K-ASHIK-L4:~/linux-command-practice/grep#
root@K-ASHIK-L4:~/linux-command-practice/grep#
root@K-ASHIK-L4:~/linux-command-practice/grep# grep -o "Case" grepPracticeLog.log
Case
Case
root@K-ASHIK-L4:~/linux-command-practice/grep#
- Print the lines not containing the string:
root@K-ASHIK-L4:~/linux-command-practice/grep# grep -v "email" grepPracticeLog.log
1. Setup Intellij IDEA
2. Download apache Jmeter latest version and extract
3. Create a java project in Intellij IDEA
5. Make a jar file from Intellij IDEA of your written project which you have performed in step 3 and 4.
6. Then place extracted jar file into your jmeter /lib/ext directory.
- Search recursively using grep -R option into all the files and directories:
gzip
gzip is used to make zip files
root@K-ASHIK-L4:~/linux-command-practice/touch# gzip csvfile.csv
root@K-ASHIK-L4:~/linux-command-practice/touch# ls
csvfile.csv.gz file1.txt file2.txt logfile.log time.txt
root@K-ASHIK-L4:~/linux-command-practice/touch# gzip -r *
root@K-ASHIK-L4:~/linux-command-practice/touch# ls
csvfile.csv.gz file1.txt.gz file2.txt.gz logfile.log.gz time.txt.gz