Linux commands to know

Linux commands to know

ยท

6 min read

ls command

The ls command is used in Linux and Unix-based operating systems to list files and directories in a directory. Here are some commonly used options with the ls command:

  1. ls - to show content inside a directory

  2. ls -a : list hidden files also with other files

  3. ls -l : list content with long details

  4. ls -R : also shows content available in sub directory

cd command

The cd command is used in Linux and Unix-based operating systems to change the current working directory. It allows you to navigate through the file system and access different directories.

  1. cd: Without any arguments, the cd command takes you to your home directory.

  2. cd <directory>: Changes the current directory to the specified directory. For example, cd Documents moves you to the "Documents" directory if it exists in the current directory.

  3. cd ..: Moves up one level in the directory hierarchy. For example, if you're in the "Documents" directory, cd .. will take you back to the parent directory.

  4. cd /: Takes you to the root directory.

  5. cd ~: Takes you to your home directory.

  6. cd -: Takes you to the previous directory you were in.

mkdir command

The mkdir command is used in Linux and Unix-based operating systems to create directories (folders). It allows you to create one or multiple directories at a time.

  1. mkdir - to make directory

  2. mkdir folder1/folder2 - creates folder 2 inside folder 1 if the folder 1 exists in current directory

  3. mkdir -p folder1/folder2 - p flag to create parent directories as needed

cat command

The cat command is used in Linux and Unix-based operating systems to concatenate and display the contents of files. It is also frequently used to create new files or append to existing ones

  1. cat name.txt - list all the things available in the text file

  2. cat > Hello - creates a hello file and we can write content into it

  3. cat file1.txt > file2.txt - copies the content of file 1 into file 2

  4. cat file1.txt file2.txt - print file 1 then file 2

  5. cat file1.txt | tr a-z A-Z > upper.txt - convert content of file 1 into uppercase using tr (translate) command and then saving into upper.txt

find command

The find command is used in Linux and Unix-based operating systems to search for files and directories within a specified directory hierarchy.

  1. find . - find files in current directory

  2. find . -type d - find the directories only inside current folder

  3. find . -type f - find files only

  4. find . -type f -name "two*" - find files that start with two ex - two.txt two.exe

  5. find . -type f -iname "two*" - not case sensitive

  6. find . -type f -mmin -20 - shows file modified less than 20 mins ago

  7. find . -type f -mmin +15 - shows file modified more than 15 mins ago

  8. find . -type f -mmin +2 --mmin -10 - modified more than 2 mins and less than 10 mins ago
    -mtime - modified time in days

  9. find . -type f -maxdepth 1 - max depth 1 means in current directory but not in sub-directory

  10. find . -type f -size +1k - search by size

  11. find . -perm 777 - find files by permission, 777 means read-write-executable

chmod command

Used to change permissions of file and directories

  1. chmod u=rwx,g=rw,o=r uppertxt - change file permission to read,write,exec for user, read,write for group, and read for others

  2. chmod 777 upper.txt - using octal numbers for read,write,execute for all three

  3. chown - used to change ownership of files and directories

  4. chown root upper.txt - error thrown as it requires sudo aslo

  5. sudo chown root upper.txt - now the file upper.txt is only accesible to root user which is provided by sudo command.

Other commands

touch

  1. touch text.txt - used to create files

  2. touch folder3/hello.txt - create hello file inside folder3

cp

  1. cp file.txt copyfile.txt - create a copy a file

  2. cp -R folder1 folder2 - create copy of folder1 and entire subtree in folder2 and runs even if error occurs.

mv

  1. mv name.txt random - moves name file into random named folder

  2. mv file1.txt file2.txt - file1 was removed and file 2 was created

rm

  1. rm copyfile.txt - permanently deletes/removes the file only

  2. rm -R copiedFolder - R flag to specify to delete the folder

df

  1. df - display info about the disk space usage in kb

  2. df -m : m flag to display info in mb

  3. df -g : to display in gb

  4. df -h : to display in human-readable format

head

  1. head - display first few lines of file

  2. head file.txt - display few no of lines decided by system

  3. head -n 4 file.txt - n flag to change the number of line to display to 4

Similarly we have tail command which just do the opposite of head command.

history

  1. history - list history of commands we used

  2. history | grep "ls" -list of ls commands we used

  3. !2742 - id of one of our previous command, doing this will run this command from the history

zip

  1. zip - to compress files and directories into a zip archive

  2. zip files.zip companies.txt - zip companies file

  3. unzip files.zip - to unzip files

  1. useradd User - to add new user

  2. passwd User - to set password

  3. userdel User - deletes a user names User

Info

To know more about a particular command just write <command name> --help you will get more possible things that we can do using a command.

Operations -

  1. & - creates a background on which some commands run so that others don't get blocked

  2. && - only when lhs in successful then only rhs will run

  3. | - send output in first command in the second command

  4. || - if lhs get executed then it's print otherwise rhs will then run

  5. ! - not operator to reverse the logic output

  6. rm -rf !(name.txt) - remove file except names.txt

  7. \>> - used to append data not overwrite it

  8. {} - combination operator used to group the commands

Best things to do with these commands is not to memorize them but play with them whenever you are dealing with terminal.


Follow -

Comment below for suggestions and improvement :) Follow me for more such content ๐Ÿ˜€. If you have any doubts you can message me, and I will respond asap :)

ย