Skip to main content

Important Linux CMD

Published 3 min readMarkdown
Important Linux CMD

Basic Linux cmd

All users should be familiar with most of these commands as they are required for most operating system tasks and computer programming.

  1. To list all files

    ls [option : -l, -a, etc]

  2. To get the present working directory

    pwd

  3. To create directory

    mkdir dir_name

  4. To navigate between directory

    cd [.., ~, /, file_path]

  5. To copy file

    cp file1 file2

  6. To rename file from file1 to file2

    mv file1.txt file2.txt

  7. To delete the files created in the file and empty the dir

    rm file.txt

  8. to delete the files created in the directory

    rm -R dir.txt

  9. To open a text editor

    touch file.txt

  10. To print/view text file

    cat file.txt

    echo file.tx

awk

awk is a programming language that originated at AT&T Bell Laboratories in 1977. Its name comes from the initials of its designers: Aho, Weinberger, and Kernighan.

The awk command uses a set of user-supplied instructions to compare a set of files, one line at a time, to extended regular expressions supplied by the user. Then actions are performed upon any line that matches the extended regular expressions.

  • awk is very useful when used with pipe, OS, grep and many more commands to extract useful information.

  • awk is best with structured data [csv]

Some common awk commands

  1. When you want to extract the top 4 processes or lines use can use

    ps -ef | awk ' FNR<=5{print}'

  2. with $ you can extract specific columns, if you want to extract 3rd column you can use $3

    ps -ef | awk ' {print $3}'

  3. To get all the data you can use $0 and to get the last column $NF [number of fields]

    ps -ef | awk ' FNR<=5{print $NF}'

  4. To Substitute words globally in the text of the file

    awk ' gsub("CMD", "command") ' <file_name>

  5. To extract a row that contains specific text

    awk ‘BEGIN {IGNORECASE=1} /TEXT/ {print $0}}‘ file_name.csv

    here IGNORECASE is a flag for case-insensitive

sed

sed is a stream editor, and it can be used to edit text files, with its most common use being to replace occurrences of words in a file. This is extremely helpful in situations where a configuration file has a lot of instances of the same word that need replacing throughout the file.

Some common sed commands

  1. To Substitute words globally in the text of the file
    sed -n ’s/TEXT1/TEXT2/g’ file_name.txt

  2. To extract specific line

    sed -n ‘2,+2p‘ file_name.txt

  3. To get an even line

    sed -n ‘2~2p‘ file_name.txt

  4. To delete empty lines in the file

    sed ‘/^$/‘ file_name.txt

  5. To get lines that contain text

    sed ‘/TEXT/p‘ file_name.txt

  6. To stop where 1st occurrence of TEXT

    sed ‘/TEXT/q‘ file_name.txt

File Permission

File permissions control who can access, modify, or execute files and directories. These permissions are categorized into three types: Read (r), Write (w), and Execute (x). They are assigned to the file's owner, the group the file belongs to, and all other users on the system

ls -l to get all files and see their permissions -r-xr-xr-x@ 1 amitgupta staff 1140643 24 Apr 14:58 res.txt here -r-xr-xr-x is permission where 1st bit is for file type d for dir and f for the file, the next 9 bits are for permission for the user, group, and other 3 bits each.

value of permission

execute x = 1

write w = 2

read r\= 4

  1. To change the permission of the file

    chmod [u/g/o][+,-][w,r,x] file_name

    chmod 0777 file_name add the value of permission

  2. To change owner

    chown [option] new_owner[:new_group] file_name

No comments yet. Be the first to comment.