Files, Directories and Permissions
This article gives an overview of files, directories and permissions on Linux, with specific reference to the information needed for the RHCSA EX200 and RHCE EX300 certification exams.
Remember, the exams are hands-on, so it doesn’t matter which method you use to achieve the result, so long as the end product is correct.
touch command is used to create a new empty file with the default permissions.
# touch /tmp/my.log -rw-r--r--. 1 root root 0 Mar 5 16:08 /tmp/my.log #
Files can also be created by output redirection using the “>” or “>>” operators.
# echo "This is going into a file." > /tmp/my2.log # cat /tmp/my2.log This is going into a file. # echo "Here is another line in the file." >> /tmp/my2.log # cat /tmp/my2.log This is going into a file. Here is another line in the file. #
vi editor can be used to create or edit files. An explanation of
vi is beyond the scope of this article, but you can find lots of information on the internet about it, such as this.
ls command lists all files and directories in the specified directory. If no location is defined it acts on the current directory. The “-a” flag lists hidden “.” files. The “-l” flag lists file details.
# ls # ls /u01 # ls -al
rm command is used to delete files and directories. The “-R” flag makes it deleted recursively and the “-f” flag stops it from prompting for confirmation.
# rm my.log # rm -Rf /archive
mv command is used to move or rename files and directories. The “.” represents the current directory.
# mv [from] [to] # mv my.log my1.log # mv * /archive # mv /archive/* .
cp command is used to copy files and directories. The “-R” flag makes it recursive.
# cp [from] [to] # cp my.log my1.log # cp * /archive # cp -R /archive/* .
find command can be used to find the location of specific files.
# find / -name dbmspool.sql # find / -print | grep -i dbmspool.sql
The “/” flag represents the staring directory for the search. Wildcards such as “dbms*” can be used for the file name.
pwd command displays the current directory.
# pwd /root #
cd command is used to change directories.
# cd /u01/app/oracle
mkdir command is used to create new directories. The “-p” flag causes it to create any missing directories in the path.
# mkdir /archive # mkdir -p /new/path/to/mydir
rmdir command is used to delete directories.
# rmdir archive
Remember, the mv and cp command apply to Navigate between directories using the
cd command described earlier.directories as well as files.
which command can be used to find the location of an executable you are using.
# which mkdir /bin/mkdir #
The “which” command searches your PATH setting for occurrences of the specified executable.
The “~” symbol represents the users home directory.
# cd ~
umask command can be used to read or set default file permissions for the current user.
# umask 022 # umask 0022 #
umask value is subtracted from the default permissions (666) to give the final permission.
666 : Default permission 022 : - umask value 644 : final permission
chmod command is used to alter file and directory permissions. The “-R” makes it recursive.
# chmod 777 *.log # chmod -R 600 /my/dir
The output below explains what permissions the the numbers represent.
Owner Group World Permission ========= ========= ========= ====================== 7 (u+rwx) 7 (g+rwx) 7 (o+rwx) read + write + execute 6 (u+rw) 6 (g+rw) 6 (o+rw) read + write 5 (u+rx) 5 (g+rx) 5 (o+rx) read + execute 4 (u+r) 4 (g+r) 4 (o+r) read only 2 (u+w) 2 (g+w) 2 (o+w) write only 1 (u+x) 1 (g+x) 1 (o+x) execute only
Character equivalents can be used in the
# chmod o+rwx *.log # chmod g+r *.log # chmod -Rx *.log
chown command is used to reset the ownership of files and directories after creation. The “-R” flag makes it recursive.
# chown oracle.oinstall /u01/myfile.txt # chown -R oracle.oinstall /u01
chgrp command is used to change just the group associated with a file or directory.
# chgrp oinstall /u01/myfile.txt # chown -R oinstall /u01
If multiple users are part of the same group, for example “dev”, setting the group of a directory to that group and setting the “g+s” flag will allow them all to share the directory. Normal permissions still apply, so the user must grant read/write permissions to the group for any files or directories they create. This is most easily done by setting
umask 002 in the “~/.bashrc” or the “~/.bash_profile” file for each user in the group.
# chgrp dev /u01/dev_share # chmod g+s /u01/dev_share
When attempting to perform certain file operations you may be presented with permissions problems.
# echo "Keep this a secret" >> /secret.txt # chmod o-r /secret.txt # su - tim_hall $ cat /secret.txt cat: /secret.txt: Permission denied $ cp /secret.txt . cp: cannot open `/secret.txt' for reading: Permission denied $
When you see these types of errors at the command line or in log files, you need to change the permissions on the file. Always try to grant the least permissions needed to allow an action to be performed.
# chmod o+r /secret.txt # su - tim_hall $ cat /secret.txt Keep this a secret $ cp /secret.txt . $
The basic permissions system can be extended using ACLs, which provide a greater level of flexibility and control. You may also need to consider ACLs, when diagnosing permissions problems.
ln command is used to create hard or soft (symbolic) links to files or directories. Directories contain links to all their child directories and files. A hard link allows you to manually create a new reference to a child file, allowing the same file to be present in multiple directories or with multiple names. There is only a single file, but it is seemingly present in multiple locations.
# echo "Test file" > /tmp/test.txt # ln /tmp/test.txt ~/test.txt # ls -al /tmp/test.txt -rw-r--r--. 2 root root 10 Mar 5 17:07 /tmp/test.txt # ls -al /root/test.txt -rw-r--r--. 2 root root 10 Mar 5 17:07 /root/test.txt #
A soft (symbolic) link is merely a pointer to a file in another location. Deleting the link does not affect the original file. Deleting the original file leave the soft link pointing at nothing.
# echo "Test file" > /tmp/test.txt # ln -s /tmp/test.txt ~/test.txt # ls -al /tmp/test.txt -rw-r--r--. 1 root root 10 Mar 5 17:09 /tmp/test.txt # ls -al /root/test.txt lrwxrwxrwx. 1 root root 13 Mar 5 17:09 /root/test.txt -> /tmp/test.txt #
For more information see:
Leave a comment