Linux

Top 10 Linux commands for Linux(CLI) users need to know.

CD Changed Directory

  • CD(Changed Directory) –
    • To change into another directory, and make it our working directory, we would use the command
cd documents/work/billing
  • To change into the root directory, make it your working directory, use :
cd /
  • (“.”) is represented by current directory. if you run this no directory traversal happened.
cd .
  • Traverse to the parent directory of your working directory, use :
cd ..
  • Jump to your home directory, use:
cd ~
  • Jump to the recent previous working directory, use:
cd -
  • Checking the path of your working directory, use:
pwd

2.CP(COPY): It copies files and folders from one directory to another.

  • You have a file named myimage.jpg in your working directory, and you want to make a copy of it called myclone-1.jpg. You would run the command
cp myimage.jpg  myclone-1.jpg
  • Some time source and destination addresses of copied files are different, so use :
cp /home/chuck/pictures/myimage.jpg /home/chuck/backup/myclone-1.jpg
  • Copying multiple files to a directory with the same type or similar type of name :
cp /pictures/myimage-*.jpg /picture-backup
  • Copying files recursively:
cp -R ~/pictures ~/picture-backup
  • Creating symbolic links without copying data
cp -s myimage.jpg myimage1.jpg
  • If you want to be prompted before overwriting a file, use the -i  option. use:
cp -i oldfile.txt newfile.txt

3. mv (move): For moves and renames files and directories.

  • For renaming a file
mv mysadimage.jpg mysmilyimage.jpg
  • For moving a file from one directory to another:
mv mysmilyimage.jpg ../myfiles
  • Move the files mysmilyimage.jpg and myfile2 to the directory myfiles:
mv -t mysmilyimage.jpg myfile1 myfile2
  • If file2 exists and is a directory, the file is moved into it. If file2 does not exist, the file is renamed file2. If file2 exists and is a file, nothing happens.
mv -n file file2
  • If file2 exists and is a file, a prompt is given:
mv: overwrite 'file2'?

Entering “y”, “yes”, “Yes”, or “Y” results in the file being overwritten. Any other input skips the file.

mv -i file file2
  • Same as mv -f. Overwrite with no prompt. The –i option is ignored.
mv -if file file2
  • Taking backup of a file with MV: If file2 exists, it is renamed to file2.bak.
mv -b --suffix=.bak file file2
  • If file2 exists, it is renamed file2.~1~. If file2.~1~ exists, it is renamed file2.~2~, etc.
VERSION_CONTROL=numbered mv -b file file2

4. CHMOD :

If no options are specified, chmod modifies the permissions of the file specified by filename to the permissions specified by permissions.

permissions define the permissions for the owner of the file (the “user”), members of the group who owns the file (the “group”), and anyone else (“others”). There are two ways to represent these permissions: with symbols or with octal numbers (the digits 0 through 7).

chmod u=rwx,g=rx,o=r

The letters ug, and o stand for “user“, “group“, and “other“. The equals sign (“=“) means “set the permissions exactly like this,” and the letters “r“, “w“, and “x” stand for “read”, “write”, and “execute”, respectively. The commas separate the different classes of permissions, and there are no spaces between them.

Here the digits 75, and 4 each individually represent the permissions for the user, group, and others, in that order. Each digit is a combination of the numbers 421, and 0:

  • 4 stands for “read”,
  • 2 stands for “write”,
  • 1 stand for “execute”, and
  • 0 stands for “no permission.”

So 7 is the combination of permissions 4+2+1 (read, write, and execute), 5 is 4+0+1 (read, no write, and execute), and 4 is 4+0+0 (read, no write, and no execute). Example:

chmod 754 myfile

2. Recursively (-R) Change the permissions of the directory myfiles, and all folders and files it contains, to mode 755. Users can read, write, and execute; group members and other users can read and execute, but cannot write.

chmod -R 755 myfiles

5. CHOWN :

chown command changes user ownership of a file, directory, or link in Linux. Every file is associated with an owning user or group. It is critical to configure file and folder permissions properly.

In this tutorial, learn how to use the Linux chown command with examples provided.

  • Change Owner of Multiple Linux Files
chown root sample2 sample3
  • Change Owner and the Group:
chown NewUser:NewGroup FILE
  • Transfer the Ownership and Group Settings from One File to Another
chown --reference=ReferenceFILE FILE
  • Check Owner and Group Before Making Changes
chown --from=CurrentUser:CurrentGroup NewUser:NewGroup FILE
  • How to Recursively Change File Ownership:
chown -R linuxuser:group3 Dir1

6. LS: used for list files or directories in Linux.

  1. Listing of all files of a particular directory
ls -l

2. Listing of all files with their details like a created date with permission set

ls -la 

3. List files in the root directory

ls /

4. List only directories

ls -d */

5. List files recursively

ls -R

6. List files in a long format including hidden files

Just type ls -l -a or ls -a -l or ls -la or ls -al command to list files or directories in a table format with extra information including hidden files.

7. Sorting files with time :

ls -t

7. Find : It is used to find files :

  1. Find files from your current directory
find  -name myimage.png

2. Find Files Under <Specific> Directory

find /<Specific> -name tecmint.txt

3. Find Directories Using dir name

find / -type d -name imgfolder

4. Find Files With 755 Permissions

find . -type f -perm 0755 -print

5. Find all the files without permission 771

find / -type f ! -perm 771

6. Find empty file sets: for directory use -d instead of -f

find /tmp -type f -empty

7. Find files modified in the last <given> date :

find /directory/path/ -mtime -<date range> -ls

8. man command:

The man command is one of the useful Linux commands. Which is also known as the manual command. The man command is just a designed help system for Linux commands. It permits individuals to search the reference manuals for authorities and other utilities executed in the interface. A command explanation, appropriate parameters, variables, illustrations, and other helpful parts may all be found on the man site (brief for handbook page). It is used to display the manual use of the inserted command. In other words, it is the meta-command of Linux. Entering the man command will display all the information about the command

man <command name>

9.  free command

The free command is used for generating information about memory usage. It calculates for both the main Random Access Memory (RAM) and swap memory. The free command shows data on the system’s utilized and unused capacity and swapped information processing. It shows RAM in kilobytes by standard (kilobytes). RAM (random access memory) and exchange storage make up the majority of memory. For example.

free -b     display information in Bytes  
free -m     display information in Megabytes  
free -g     display information in Gigabytes 
free -tDisplay total memory
free -oDisable buffer adjusted line
free -s <time>To update RAM at regular intervals
free -lDisplay high and low memory size statistics
watch freeAllows iterations
free

10. history command

The history command is most frequently used and one of the most important Linux commands.

This feature was not available in the Bourne shell. Bash and Korn support this feature in which every command executed is treated as the event and is associated with an event number using which they can be recalled and changed if required. These commands are saved in a history file.

To show the limited number of commands that executed previously as follows

 history 10 

RECOMMENDED ARTICLES





Leave a Reply

Your email address will not be published. Required fields are marked *