File operations in Linux can be done by using many commands that already provided.
To create a new file, use
touch command. This is the example of command usage.
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
There are many ways to read a file, the first way is using installed text editor like
nano and so on. This is the example of read a file using
nano text editor.
The second way of reading a file is using
The file's content is printed in the terminal when using
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
- Open a file with
To view a list of files can be done by using
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
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
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
cp file_2.txt mydirectory
To remove certain file, use
rm command followed with file name. In this example, the file called
file_2.txt will be removed.
To remove a directory, use
rmdir command followed with directory name.
To remove a directory and its content. Use
rm -rf directory_name
This access control also available for directory
There are three types of user in file's access control:
- Owner user:
- Other user:
There are three types of file control:
- read: read or open the file (
- write: write inside the file (
- execute: execute a file (
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.
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:
||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
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.
0 represents access is not available, when the number
1 represents access is available.
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
When you want to learn more about specific commands, just add
--help after specific command.
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.