Linux is a operating system like OS X and Windows, however much more lightweight. It is built upon UNIX, which is an open-source operating system. Two of the most common distribution of Linux are Ubuntu and Debian.
To search every txt files: find / -name "*.txt". Remember to use quotes.
To search files whose name starts with fi: find / -name "fi*"
grep
grep shorts for Global Regular Expression Print. It searches for a pattern in a file or files.
let’s we have a file named file1.txt with the following content:
1
2
This is file1 with example content.
this is the line 2.
To search for the word example in the file, use the following command:
1
2
user@linux:~$ grep "example" file1.txt
This is file1 with example content.
Option
Description
-i
Ignores case.
-n
Displays line numbers.
-c
Displays the count of matching lines.
-r
Searches recursively in directories.
-lr
Displays only the names of files with matching lines, recursively.
head and tail
head Displays the first few lines of a file.
head file1.txt displays the first 10 lines of the file.
head -n 5 file1.txt displays the first 5 lines of the file.
tail Displays the last few lines of a file.
tail file1.txt displays the last 10 lines of the file.
tail -n 5 file1.txt displays the last 5 lines of the file.
less
Displays the contents of a file one page at a time.
less file1.txt displays the contents of the file.
Press q to exit the less command.
Introduction to shell operators
&
Runs a command in the background. command &
&&
Runs a command only if the previous command was successful. command1 && command2
>
Redirects the output of a command to a file. If the file already exists, it is overwritten. command > file1.txt
»
Appends the output of a command to a file. If the file already exists, the output is appended to the end. command >> file1.txt
|
Pipes the output of one command to another command.
command1 | command2 pipes the output of command1 to command2.
ls -l | grep ".txt" lists all files in the current directory and pipes the output to grep to search for files with the .txt extension.
ps aux --sort=-%mem | head -n 5 lists the top 5 processes consuming the most memory.
du -h | sort -rh | head -n 5 lists the top 5 directories consuming the most disk space.
File Permissions in Linux
In Linux, each file and directory has three types of permissions: read, write, and execute. These permissions are assigned to three entities: the owner, the group, and others.
To view the permissions of a file or directory, use the ls -l command.
1
2
user@linux:~$ ls -l file1.txt
-rw-r--r-- 1 user user 0 Jun 12 00:37 file1.txt
- indicates a regular file. rw- indicates that the owner has read and write permissions. r-- indicates that the group has read permissions. r-- indicates that others have read permissions.
Numeric values are used to represent permissions.
r is represented by 4, w is represented by 2, and x is represented by 1.
rwx is represented by 7, rw- is represented by 6, r-x is represented by 5, and r-- is represented by 4, --- is represented by 0.
Some Basic Permissions:
Symbolic Representation
Numeric Value
Description
rw-r–r–
644
Read and write permissions for the owner, read permissions for the group and others.
rwxr-xr-x
755
Read, write, and execute permissions for the owner, read and execute permissions for the group and others.
rwxrwxrwx
777
Read, write, and execute permissions for the owner, group, and others.
rw-rw-rw-
666
Read and write permissions for the owner, group, and others.
rwx——
700
Read, write, and execute permissions for the owner only.
r——–
400
Read permissions for the owner only.
r-x——
500
Read and execute permissions for the owner only.
chmod
Changes the permissions of a file or directory.
chmod 777 file1.txt grants read, write, and execute permissions to the owner, group, and others.
chmod 755 file1.txt grants read, write, and execute permissions to the owner and read and execute permissions to the group and others.
chown
Changes the owner of a file or directory.
chown user1 file1.txt changes the owner of the file to user1.
chown user1:group1 file1.txt changes the owner and group of the file to user1 and group1.
Common Directories
/etc - Contains system-wide configuration files.
/var - Contains variable data files, such as logs and databases.
/tmp - Contains temporary files. Once the system is rebooted, the files are deleted.