Basic Commands

·

10 min read

  1. ls - The most frequently used command in Linux to list directories

  2. pwd - Print working directory command in Linux

  3. cd - Linux command to navigate through directories

  4. mkdir - Command used to create directories in Linux

  5. mv - Move or rename files in Linux

  6. cp - Similar usage as mv but for copying files in Linux

  7. rm - Delete files or directories

  8. touch - Create blank/empty files

  9. ln - Create symbolic links (shortcuts) to other files

  10. cat - Display file contents on the terminal

  11. clear - Clear the terminal display

  12. echo - Print any text that follows the command

  13. less - Linux command to display paged outputs in the terminal

  14. man - Access manual pages for all Linux commands

  15. uname - Linux command to get basic information about the OS

  16. whoami - Get the active username

  17. tar - Command to extract and compress files in Linux

  18. grep - Search for a string within an output

  19. head - Return the specified number of lines from the top

  20. tail - Return the specified number of lines from the bottom

  21. diff - Find the difference between two files

  22. cmp - Allows you to check if two files are identical

  23. comm - Combines the functionality of diff and cmp

  24. sort - Linux command to sort the content of a file while outputting

  25. export - Export environment variables in Linux

  26. zip - Zip files in Linux

  27. unzip - Unzip files in Linux

  28. ssh - Secure Shell command in Linux

  29. service - Linux command to start and stop services

  30. ps - Display active processes

  31. kill and killall - Kill active processes by process ID or name

  32. df - Display disk filesystem information

  33. mount - Mount file systems in Linux

  34. chmod - Command to change file permissions

  35. chown - Command for granting ownership of files or folders

  36. ifconfig - Display network interfaces and IP addresses

  37. traceroute - Trace all the network hops to reach the destination

  38. wget - Direct download files from the internet

  39. ufw - Firewall command

  40. iptables - Base firewall for all other firewall utilities to interface with

  41. apt, pacman, yum, rpm - Package managers depending on the distro

  42. sudo - Command to escalate privileges in Linux

  43. cal - View a command-line calendar

  44. alias - Create custom shortcuts for your regularly used commands

  45. dd - Majorly used for creating bootable USB sticks

  46. whereis - Locate the binary, source, and manual pages for a command

  47. whatis - Find what a command is used for

  48. top - View active processes live with their system usage

  49. useradd and usermod - Add new user or change existing users data

  50. passwd - Create or update passwords for existing users

further variations:-

Let's dive deep into the commands...

  1. pwd

    displays your present working directory/current directory

  2. cd

    used to change your current directory

  3. cd..

    it takes us to the parent directory

    (base) dazaisan@MSI-Modern-14-A10RAS:~/Downloads$ pwd
    /home/dazaisan/Downloads

    (base) dazaisan@MSI-Modern-14-A10RAS:~/Downloads$ cd ..

    (base) dazaisan@MSI-Modern-14-A10RAS:~$ pwd
    /home/dazaisan

  4. cd -

    it takes us to the previous directory

    (base) dazaisan@MSI-Modern-14-A10RAS:~/Downloads$ pwd
    /home/dazaisan/Downloads

    (base) dazaisan@MSI-Modern-14-A10RAS:~/Downloads$ cd ..

    (base) dazaisan@MSI-Modern-14-A10RAS:~$ pwd
    /home/dazaisan

    (base) dazaisan@MSI-Modern-14-A10RAS:~$ cd -
    /home/dazaisan/Downloads

  5. ls

    it lists the contents of the directory in which it is used

  6. ls -a

    it shows all the hidden files

  7. ls -l

    lists the permissions of the files and directories as well as other attributes such as folder names, file and directory sizes, and modified date and time

  8. ls -r

    lists files in reverse order

  9. ls -lh

    used to easily identify the file sizes as kilobytes (kB), Megabytes (MB) or Gigabytes (GB)

  10. ls -R

    used to display the directory trees of files and folders

  11. ls -F

    used to distinguish files from directories

    dazaisan@MSI-Modern-14-A10RAS:~$ls -F

    a1.c a.out* Documents/ mysql p1.out* p3.c Pictures/ snap/ add.tcl cn1.tcl Downloads/ out.nam p2.c p3.cpp Public/ Templates/ anaconda3/ Desktop/ Music/ p1.c p2.out* p4.c q1.c Videos/

  12. ls -i

    it is used to display the inode number of files and directories

  13. ls -n

    used when you want to display the UID as well as the GId of files and directories

  14. ls --v
    for ls version
    ls --help
    to view options about what you can do with ls

  15. alias and unalias

    (base) dazaisan@MSI-Modern-14-A10RAS:~$ alias

    alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '''s/^\s*[0-9]+\s*//;s/[;&|]\salert$//''')"' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l='ls -CF' alias la='ls -A' alias ll='ls -alF' alias ls='ls --color=auto'

    (base) dazaisan@MSI-Modern-14-A10RAS:~$ unalias la #la alias is removed

    (base) dazaisan@MSI-Modern-14-A10RAS:~$ alias

    alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '''s/^\s[0-9]+\s*//;s/[;&|]\s*alert$//''')"' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l='ls -CF' alias ll='ls -alF' alias ls='ls --color=auto'

  16. man ls

    manual for ls command uses is displayed.

  17. mkdir
    used to make a new directory, at least one parameter is needed which is the name of the new directory to be created.

  18. mkdir -p

    it solves the following problem

    dazaisan@MSI-Modern-14-A10RAS:~/Desktop$ mkdir test/linux/t1 mkdir: cannot create directory ‘test/linux/t1’: No such file or directory

    The command fails because the parent directory of threedirsdeep does not exist.

    the option -p, then mkdir will create parent directories as needed.
    (base) dazaisan@MSI-Modern-14-A10RAS:~/Desktop$ mkdir -p test/linux/t1 using -p, there will be no issue while creating a new directory inside a directory which doesn't exist as the new directories are created as per the requirement

  19. rmdir

    it is used to remove the directory. Parameter has the path of the directory to be removed.

    Note: The directory should be empty before using this command

  20. rmdir -p
    to recursively remove directories. it deletes multiple directories at once.
    Note: The directory should be empty before using this command

    I'll keep updating commands as i encounter the new one's. Thanks for reading :)

    I have tried to add some extra stuffs in Knowledge stack section

Knowledge Stack:

An inode is a data structure that keeps track of all the files and directories within a Linux or UNIX-based filesystem. So, every file and directory in a filesystem is allocated an inode, which is identified by an integer known as an “inode number”. These unique identifiers store metadata about each file and directory. All inodes within the same filesystem are unique.

  1. Each inode is identified by an inode number. Therefore, when creating or copying a file, Linux assigns a different inode number to the new file. However, when moving a file, the inode number will only change if the file is moved to a different filesystem. This applies to directories as well.

    Use df-i to pull basic data about your inode usage, including the file system on which the inodes are stored, your total inode count, how many are in use (in count and %), and how many remain.

    To check the number of inodes in a directory, the wc command is used to count the number of characters, words, lines and bytes of files. Together with the -l option, you can use it to count the number of inodes in a directory

    to check inode uses, https://www.2daygeek.com/linux-check-count-inode-usage/

    Counting inod enumber with grand total

    (base) dazaisan@MSI-Modern-14-A10RAS:~$ echo "Detailed Inode usage for: $(pwd)" ; for d in find -maxdepth 1 -type d |cut -d\/ -f2 |grep -xv . |sort; do c=$(find $d |wc -l) ; printf "$c\t\t- $d\n" ; done ; printf "Total: \t\t$(find $(pwd) | wc -l)\n"

    Detailed Inode usage for: /home/dazaisan 171545 - anaconda3 4 - .anydesk 50892 - .cache 2 - .conda 25716 - .config 3327 - Desktop 2 - Documents 8 - .dotnet 2 - Downloads 3 - .gnupg 1 - .gphoto 16 - .java 3604 - .local 12 - .minikube 2 - .mozilla 1 - Music 21 - .npm 17 - .openshot_qt 25 - Pictures 5 - .pki 1 - Public 4 - .rpmdb 12982 - snap 1 - .ssh 1 - Templates 114 - .thunderbird 2 - Videos 7204 - .vscode 67 - .zoom Total: 275604

    Metadata stored in an inode

    Inodes store metadata such as:

    • File type

    • File size

    • Owner ID

    • Group ID

    • Read, write and execute permissions

    • Last access time

    • Last change time

    • Last modification time

Excessive inode usage can lead to issues when creating new files and directories. Some of the issues users may encounter when the server runs out of inodes are:

  • Data loss.

  • Server restart.

  • Application crash.

  • Scheduled tasks not running.

So, it is recommended to keep inode usage low by deleting:

  • Unnecessary files and directories.

  • Cache files.

  • Old email files.

  • Temporary files.

  1. In a Unix system, a GID (group ID) is a name that associates a system user with other users sharing something in common (perhaps a work project or a department name). It's often used for accounting purposes. A user can be a member of more than one group and thus have more than one GID. Any user using a UNIX system at a given time has both a user ID (UID) and a group ID (GID).

PRACTICE EXERCISE reference Linux Fundamentals- Paul Cobbaut

  1. Display your current directory.

  2. Change to the /etc directory.

  3. Now change to your home directory using only three key presses.

  4. Change to the /boot/grub directory using only eleven key presses.

  5. Go to the parent directory of the current directory.

  6. Go to the root directory.

  7. List the contents of the root directory.

  8. List a long listing of the root directory.

  9. Stay where you are, and list the contents of /etc.

  10. Stay where you are, and list the contents of /bin and /sbin.

  11. Stay where you are, and list the contents of ~.

  12. List all the files (including hidden files) in your home directory.

  13. List the files in /boot in a human readable format.

  14. Create a directory testdir in your home directory.

  15. Change to the /etc directory, stay here and create a directory newdir in your home directory.

  16. Create in one command the directories ~/dir1/dir2/dir3 (dir3 is a subdirectory from dir2, and dir2 is a subdirectory from dir1 ).

  17. Remove the directory testdir.

  18. If time permits (or if you are waiting for other students to finish this practice), use and understand pushd and popd. Use the man page of bash to find information about these commands

solution: working with directories

  1. Display your current directory. pwd

  2. Change to the /etc directory. cd /etc

  3. Now change to your home directory using only three key presses. cd (and the enter key)

  4. Change to the /boot/grub directory using only eleven key presses. cd /boot/grub (use the tab key)

  5. Go to the parent directory of the current directory. cd .. (with space between cd and ..)

  6. Go to the root directory. cd /

  7. List the contents of the root directory. ls

  8. List a long listing of the root directory. ls -l

  9. Stay where you are, and list the contents of /etc. ls /etc

  10. Stay where you are, and list the contents of /bin and /sbin. ls /bin /sbin

  11. Stay where you are, and list the contents of ~. ls ~

  12. List all the files (including hidden files) in your home directory. ls -al ~

  13. List the files in /boot in ahuman readable format. ls -lh /boot 14. Create a directory testdir in your home directory. mkdir ~/testdir 15. Change to the /etc directory, stay here and create a directory newdir in your home directory.

    cd /etc ; mkdir ~/newdir

  14. Create in one command the directories ~/dir1/dir2/dir3 (dir3 is a subdirectory from dir2, and dir2 is a subdirectory from dir1 ). mkdir -p ~/dir1/dir2/dir3

  15. Remove the directory testdir. rmdir testdir

  16. If time permits (or if you are waiting for other students to finish this practice), use and understand pushd and popd. Use the man page of bash to find information about these commands.

    man bash # opens the manual

    /pushd # searches for pushd

    n # next (do this 2-3 times)

The Bash shell has two built-in commands called pushd and popd. Both commands work with a common stack of previous directories. Pushd adds a directory to the stack and changes to a new current directory, popd removes a directory from the stack and sets the current directory.

paul@debian7:/etc$ cd /bin
paul@debian7:/bin$ pushd /lib
/lib /bin
paul@debian7:/lib$ pushd /proc
/proc /lib /bin
paul@debian7:/proc$ popd
/lib /bin
paul@debian7:/lib$ popd
/bin

References:

[1] https://www.stackscale.com/blog/inodes-linux/#:~:text=An%20inode%20is%20a%20data,about%20each%20file%20and%20directory.

[2] https://www.techtarget.com/whatis/definition/GID-group-ID-or-global-index-file

[3] https://www.digitalocean.com/community/tutorials/linux-commands