File Operations in Linux
File operations in Linux can be done by using many commands that already provided.
Create a new file and directory
To create a new file, use touch command. This is the example of command usage.
touch file.txt
To create multiple files, can be done with the help of &&.
touch file_2.txt && touch file_3.txt
To create a new directory, use mkdir command.
mkdir mydirectory
Read a file
There are many ways to read a file, the first way is using installed text editor like vim, nano and so on. This is the example of read a file using nano text editor.
nano file.txt
The second way of reading a file is using cat or more command.
cat file.txt
The file's content is printed in the terminal when using cat command.
Edit a file
To edit a file can be done using text editor or related application with the file's type. In this example, the file with .txt extension is edited with text editor using nano.
- Open a file with
nanocommand.
nano file.txt
View files
To view a list of files can be done by using ls command.
To view a list of files with specific information. use ll command. This command outputs all files with specific information.
Here it is the information detail from ll command output.
-rw-r--r-- 1 nadir nadir 0 Jan 8 10:24 iamfile.txt
access control number of link owner group size time created file name
To view a specific file information, use ll | grep filename command.
ll | grep iamfile.txt
Move and Copy file
To move a certain file, use mv command followed with target file and target directory. In this example, the file called file.txt moved to the mydirectory directory.
mv file.txt mydirectory
To copy a certain file, use cp command followed with target file and target directory. In this example, the file called file_2.txt copied to the mydirectory directory.
cp file_2.txt mydirectory
Remove file and directory
To remove certain file, use rm command followed with file name. In this example, the file called file_2.txt will be removed.
rm file_2.txt
To remove a directory, use rmdir command followed with directory name.
rmdir directory
To remove a directory and its content. Use rm -rf.
rm -rf directory_name
Access Control in file
This access control also available for directory
There are three types of user in file's access control:
- Owner user:
u - Group:
g - Other user:
o
There are three types of file control:
- read: read or open the file (
r) - write: write inside the file (
w) - execute: execute a file (
x)
Access control mechanism can be done using chmod command followed with specific parameters to define the access control type. There are two ways to define the access control type.
1. Using characters
In this example, the file's access control defined using characters.
chmod go-w cool.txt
Based on the command above, the cool.txt file can be written, read and executed by user. The group and other user cannot write or edit the file, but available for read operation.
The complete formula of this command can be seen below:
| Operation | Meaning |
|---|---|
+ |
add access type |
- |
remove access type |
= |
available access type |
chmod [user's type (u, g, o, a)] [-, +, =] [access type (r, w, x)]
ameans the all user type.
Example, user only available for reading the file.
chmod u=r hey.txt
Another example, user only capable to read file, group available to write and other can read and write except execute the file:
chmod u=r,g+w,o-x cool.txt
2. Using numbers.
The number that used in file's access control is using binary number to define the access type of certain file. This is the sample usage.
chmod 744 cool.txt
Based on the command above, the user is available for all file's operation (read, write and execute), the group and the other user is only available to read the file.
The number 0 represents access is not available, when the number 1 represents access is available.
| number | read | write | execute |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 1 |
| 2 | 0 | 1 | 0 |
| 3 | 0 | 1 | 1 |
| 4 | 1 | 0 | 0 |
| 5 | 1 | 0 | 1 |
| 6 | 1 | 1 | 0 |
| 7 | 1 | 1 | 1 |
File ownership
To change the file or directory ownership, use chown command followed with user's name.
chown cool.txt other_user
To change the file or directory ownership for group, use chgrp followed with group's name.
chgrp cool.txt mygroup
Tips
When you want to learn more about specific commands, just add --help after specific command.
ls --help
I hope this article is helpful for learning about linux command especially for file operations. If you have any thoughts or feedbacks, you can write in the discussion section below.




Top comments (0)