Linux Commands for DevOps: The Ultimate Guide to Optimizing Your Workflow and Managing Systems

Table of contents

No heading

No headings in the article.

As a DevOps professional, the command line is your best friend. Linux commands allow you to automate repetitive tasks, manage system resources, and troubleshoot issues quickly and efficiently. In this article, we'll go over some essential Linux commands that every DevOps engineer should know.

1. pwd: Display the name of the current working directory

Example:

rachana-uniyal@rachanauniyal:~$ pwd
/home/rachana-uniyal
rachana-uniyal@rachanauniyal:~$

2. ls: List the files in a directory

Example:


rachana-uniyal@rachanauniyal:~$ ls
Downloads  Public    Templates
Desktop    package   Recordings  Videos
Documents  Music     Pictures  snap
rachana-uniyal@rachanauniyal:~$

2.1 ls path: List the files in a specific directory

Example:

rachana-uniyal@rachanauniyal:~$ ls Downloads
 days     IMG_1618.jpg                                                                            QnA.pdf   Photos  myapp  
rachana-uniyal@rachanauniyal:~$

2.2 ls -R path: List all files in a directory and its subdirectories

Example:

rachana-uniyal@rachanauniyal:~$ ls -R Pictures/
Pictures/:
'Screenshot from 2022-10-17 16-31-55.png'   snapshots

Pictures/snapshots:
'Screenshot from 2022-12-21 12-25-12.png'
'Screenshot from 2022-12-21 13-22-44.png'
'Screenshot from 2022-12-22 11-58-09.png'
rachana-uniyal@rachanauniyal:~$

2.3 ls -a: List all files, including hidden files

Example:

rachana-uniyal@rachanauniyal:~$ ls -a
.              .cache        .gradle         .pki            
.config        .java         .profile        .vscode
config         .local        Public          .wget-hsts
Desktop        .m2           .rbenv          .xournal
Documents      .mozilla      Recordings      .yarn
Downloads      Music         .s2.sh.swp      .yarnrc
rachana-uniyal@rachanauniyal:~$

3. cd: Change the current working directory

Example:

rachana-uniyal@rachanauniyal:~$ cd Pictures/
rachana-uniyal@rachanauniyal:~/Pictures$ pwd
/home/rachana-uniyal/Pictures
rachana-uniyal@rachanauniyal:~$

4. mkdir: Create a new directory

Example:

rachana-uniyal@rachanauniyal:~/Pictures$ mkdir newDirectory
rachana-uniyal@rachanauniyal:~$

5. touch: Create an empty file

Example:

rachana-uniyal@rachanauniyal:~/Pictures/newDirectory$ touch newFile.txt
rachana-uniyal@rachanauniyal:~$

6. rm: Delete a file

Example:

rachana-uniyal@rachanauniyal:~/Pictures/newDirectory$ rm newFile.txt 
rachana-uniyal@rachanauniyal:~$

6.1 rm -r: Delete a directory and its contents

Example:

rachana-uniyal@rachanauniyal:~/Pictures$ rm -r newDirectory/
rachana-uniyal@rachanauniyal:~$

6.2 rm -d or rmdir: Delete an empty directory

Example:

rachana-uniyal@rachanauniyal:~/Pictures$ rm -d newDirectory/
rachana-uniyal@rachanauniyal:~$

7. cd /: Change to the root directory

Example:

rachana-uniyal@rachanauniyal:~/Pictures$ cd /
rachana-uniyal@rachanauniyal:/$ pwd
/
rachana-uniyal@rachanauniyal:~$

8. clear: Clear the terminal screen

Example:

rachana-uniyal@rachanauniyal:/$ clear
rachana-uniyal@rachanauniyal:~$

9. mv: Rename a file

Example:

rachana-uniyal@rachanauniyal:~$ mv oldname.txt newname.txt
rachana-uniyal@rachanauniyal:~$

10. cp: Copy a file or directory

Example:

rachana-uniyal@rachanauniyal:~$ cp file.txt /newlocation/
rachana-uniyal@rachanauniyal:~$

11. history: Display a list of previously used commands

Example:

rachana-uniyal@rachanauniyal:~$ history
 1858  pwd
 1859  ls
 1860  ls /Downloads
 1861  ls
 1862  ls Downloads
 1863  ls -R Documents/
 1864  clear
 1865  ls -R Pictures/
 1866  ls -a
 1867  cd Pictures/
rachana-uniyal@rachanauniyal:~$

11.1 history n: - Display the last n commands used

Example:

rachana-uniyal@rachanauniyal:~$ history 5
 1879  rm -d newDirectory/
 1880  cd /
 1881  pwd
 1882  history
 1883  history 5
rachana-uniyal@rachanauniyal:~$

12. ctrl + r: Search for a previously used command

Press ctrl + r, then type the command you are searching for.

rachana-uniyal@rachanauniyal:~$ (reverse-i-search)`cd': cd /

13. ctrl + c: Stop the current command

Press ctrl + c to stop the command that is currently running.

14. cat: Display the contents of a file

Example:

rachana-uniyal@rachanauniyal:~$ cat file.txt
Hi, how are you doing today?
rachana-uniyal@rachanauniyal:~$

15. uname -a: Display system and kernel information

Example:

rachana-uniyal@rachanauniyal:~$ uname -a
Linux rachanauniyal 5.15.0-57-generic #63~20.04.1-Ubuntu SMP Wed Nov 30 13:40:16 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
rachana-uniyal@rachanauniyal:~$

16. cat /etc/os-release: Display system release information

Example:

rachana-uniyal@rachanauniyal:~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.5 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.5 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
rachana-uniyal@rachanauniyal:~$

17. lscpu: Display CPU information

Example:

rachana-uniyal@rachanauniyal:~$ lscpu
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   39 bits physical, 48 bits virtual
CPU(s):                          8
On-line CPU(s) list:             0-7
Thread(s) per core:              2
Core(s) per socket:              4
Socket(s):                       1
NUMA node(s):                    1
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           142
Model name:                      Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
Stepping:                        10
CPU MHz:                         1800.000
CPU max MHz:                     1600.0000
CPU min MHz:                     400.0000
BogoMIPS:                        3600.00
Virtualization:                  VT-x
L1d cache:                       128 KiB
L1i cache:                       128 KiB
L2 cache:                        1 MiB
L3 cache:                        6 MiB
NUMA node0 CPU(s):               0-7
Vulnerability Itlb multihit:     KVM: Mitigation: VMX disabled
Vulnerability L1tf:              Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds:               Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown:          Mitigation; PTI
Vulnerability Mmio stale data:   Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed:          Mitigation; IBRS
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds:             Mitigation; Microcode
Vulnerability Tsx async abort:   Not affected
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss
                                  ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonsto
                                 p_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse
                                 4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_f
                                 ault invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust
                                  bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dthe
                                 rm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp md_clear flush_l1d arch_capabilities
rachana-uniyal@rachanauniyal:~$

18. lsmem: Display memory information

Example:

rachana-uniyal@rachanauniyal:~$ lsmem
RANGE                                  SIZE  STATE REMOVABLE BLOCK
0x0000000000000000-0x000000008fffffff  2.3G online       yes  0-17
0x0000000100000000-0x000000026fffffff  5.8G online       yes 32-77

Memory block size:       128M
Total online memory:       8G
Total offline memory:      0B
rachana-uniyal@rachanauniyal:~$

19. sudo: Execute a command as the superuser

Example:

rachana-uniyal@rachanauniyal:~$ sudo apt-get update
Hit:1 https://deb.nodesource.com/node_16.x focal InRelease
Hit:2 https://dl.google.com/linux/chrome/deb stable InRelease                                                                     
Hit:3 http://security.ubuntu.com/ubuntu focal-security InRelease                                                                  
Hit:4 http://in.archive.ubuntu.com/ubuntu focal InRelease                      
Hit:5 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:6 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease
Reading package lists... Done
rachana-uniyal@rachanauniyal:~$

20. su: Switch to a different user

Example:

rachana-uniyal@rachanauniyal:~$ su dev
Password: 
dev@rachanauniyal:/home/rachana-uniyal$

21. "|": Pipe the output of one command to another

Example:

rachana-uniyal@rachanauniyal:~$ ls | less
config
Desktop
Documents
Downloads
Music
package
Pictures
~
~
~
~
~
~
~
~
~
~
(END)

22. ">": Redirect the output of a command to a file

Example:

rachana-uniyal@rachanauniyal:~$ ls > fileList.txt
rachana-uniyal@rachanauniyal:~$ cat fileList.txt 
config
Desktop
Documents
Downloads
rachana-uniyal@rachanauniyal:~$

23. grep: Search for a pattern in a file

Example:

rachana-uniyal@rachanauniyal:~$ grep file fileList.txt 
fileList.txt
file.txt
rachana-uniyal@rachanauniyal:~$

24. less: View a file a page at a time

Example:

rachana-uniyal@rachanauniyal:~$ less fileList.txt 
config
Desktop
Documents
Downloads
fileList.txt
file.txt
Music

fileList.txt (END)