Essential Linux Commands for File and System Management
Classified in Technology
Written on in
English with a size of 2.65 KB
More Commands to Query the System
The chmod Command
This command is used to modify file permissions. There are two primary ways of using it:
- Octal (Numeric) Notation:
chmod 777 myfile - Symbolic Notation:
chmod g+x myfile
In the first case, we set permissions using the octal form, while in the second, we use symbolic notation. In the example, we add the execute permission (x) to the group (g) owning the file. We can use + to add or - to remove permissions for different classes:
- u: User (owner)
- g: Group
- o: Others
These are followed by the permission type: r (read), w (write), or x (execute).
The chown Command
This command is used to change the owner of a file and can be used as follows:
chown -R new_owner /path/to/fileThe chgrp Command
The chgrp command changes the group that owns the file or directory.
chgrp new_group /path/to/fileThe du Command
This command displays the disk usage of each file and directory, including subdirectories.
duIf you want to view only the total size of all files and directories in a folder, you can use this command:
du -ch | grep totalThe grep Command
This indispensable command allows you to search for text strings within a file or another command's output. For example, the following command chain will return the number of directories in the current directory (ls -l lists files and directories, and wc -l counts the number of lines):
ls -l | grep ^d | wc -lIn another example, we can see the number of times the root user is connected, which is useful if you have multiple shells open simultaneously (the who command returns a string with all connected users):
who | grep root | wc -lThe grep command's true power comes from its support for regular expressions, although they can add complexity. Suppose we have a file named test_file with the following information:
dir 15kb 27/07/2007
27kb file 26/07/2007
dualco 1kb 26/07/2007If we want to display only the lines that start with the letter 'd', this would be the solution using grep with a regular expression:
grep '^d' test_file