Linux Essential Commands - files and directories
Read, and use System Documentation
Manual Pages With man Command
man printf
man 1 printf
man 3 printf
# section numbers of the manual followed by the types
of pages they contain
1 Executable programs or shell commands
2 System calls (functions provided by the kernel)
3 Library calls (functions within program libraries)
...
apropos command helps the user when they don’t remember the exact command but knows a few keywords related to the command that define its uses or functionality
# Might have to run this command before
sudo mandb
# Find commands related to find action
apropos find
# Find commands related to compress action
apropos compress
Create, Delete, Copy, and Move Files and Directories
pwd # print working directory
cd - # Go to previous directory
touch FILE_TO_BE_CREATED
mkdir DIRECTORY_TO_BE_CREATED
mkdir -p DIRECTORY_TO_BE_CREATED/DIRECTORY_TO_BE_CREATED_01/DIRECTORY_TO_BE_CREATED_02
ls -lah
cp -r SOURCE DEST
cp -rpv SOURCE DEST # preserve attributes
mv SOURCE DEST
rm -rf FILE_DIR_TO_BE_REMOVED
Hard links and symbolic links
- Hard links are for files only; soft link are for both files and directories
- Hard link: "ln source_file dest_file"
+ Same filesystem only
+ Creates another file with a link to the same underlying inode. Deleting, renaming, or moving the original file will not affect the hard link as it links to the underlying inode
- Soft link: "ln -s source_file dest_file"
+ Different filesystems are ok
+ Creates a link to original file. The link will not work if modifying original file.
List, set, and change standard file and directory permissions
- r w x r w x r w x. 1 aaron family 49 Oct 27 14:41 family_dog.jpg
The first symbol '-' mean
- file
d directory
c character of device
l link
s socket file
p pipe
b block device
# Add remove and set permissions
# Add rw to user, remove w from group, set r to other
chmod u+rw,g-w,o=r MY_FILE
# Add exec to owner, group and other
chmod +x file
# or
chmod a+x file
# Octal Permissions
# r = 4
# w = 2
# x = 1
# Permissions: 664 = u+rw,g+rw,o+r
# These two commands are same
chmod u=rw,g=r,o= family_dog.jpg
chmod 640 family_dog.jpg
ls -l
-rw-r-----. 1 aaron family 49 Oct 27 14:41 family_dog.jpg
SUID, SGID, and Sticky Bit
- Sticky bit : allows only the owner of the file within that directory or the root user to delete or rename the file. Commands:
chmod +t /usr/local/tmp # set the sticky bit on a directory
chmod 1777 /usr/local/tmp # same as above, using octal notation with 1 at the first
- suid : Run as owner of file
chmod u+s file
chmod 4664 file # add 4 at the first
- sgid : Run as group owner
chmod g+s file
chmod 2664 file # add 2 at the first
# Add the permissions for setuid, setgid and sticky bit on /home/bob/datadir directory
chmod u+s,g+s,o+t /home/bob/datadir
Search for files
find command
# find all .pdf files
find /usr/share/doc -name '*.pdf'
# use -iname for case-insensitive search for .pdf or .PDF
find . -iname '*.pdf'
# find all files which are not .pdf
find . -not -name '*.pdf'
find . ! -name '*.pdf'
# find all files which are .pdf or .docx
find . ( -name '*.pdf' -o -name '*.docx' )
# execute a copy on the output from the find - to copy all docs to the current directory
find /usr/share/doc -name '*.pdf' -exec cp {} . ;
# delete based on output from find
find . -name '*.pdf' -delete
# find file of type link
find /etc -type l
# find file
find /etc -type f
# find directory
find /etc -type d
# limit to just that directory, not below
find /etc -maxdepth 1 -type f
# find empty file
find /tmp -type f -empty
# look for files by size
find /boot -size +20000k -type f
find /boot -size +20000k -type f -exec du -h {} ;
# find files which have size between 2M to 4M, and find in 3 level of sub directories
find . -maxdepth 3 -type f -size +2M -size -4M
# find files which "have size > 2M" or "names have .log", and find in 3 level of sub directories
find . -maxdepth 3 -type f ( -size +2M -o -name "*.log" )
# find all 100MB files and delete them using one single command.
find / -type f -size +100M -exec rm -f {} ;
# Find all files between 5mb and 10mb in the /usr directory
find /usr -type f -size +5M -size -10M
# "Modify" is the timestamp of the last time the file's content has been modified which is often called "mtime".
# "Change" is the timestamp of the last time the file's inode has been changed, like by changing permissions, ownership, file name, number of hard links. It's often called "ctime".
# -mmin modified for minutes
# -mtime modified for days (24 hours)
# -atime Accessed Files
# -cmin changed for minutes
# -ctime changed for days
# Find files that are exactly 2 minutes old, or modified exactly 2 minutes ago
find . -mmin 2
# Find files that are less than 2 minutes old, or modified since 2 minutes ago then now
find . -mmin -2
# Find files that are more than 2 minutes old, or modified before 2 minutes from now
find . -mmin +2
# find all the files which are modified more than 50 days back and less than 100 days
find / -mtime +50 –mtime -100
# find all the files which are accessed 50 days back
find / -atime 50
#Find all the files which have been modified in the last 2 hours in /usr directory
find /usr -type f -mmin -120
# Special case of 0
+0 - all files older than now , which will be all files in your directory
-0 - all files newer than now , means no files
0 - all files modified right now - you will get /root/.bash_history if you are doing it in /root folder as it gets modified instantly
# find all file based on owner
find /home -user user_name
# Find all Files Based on Group
find /home -group group_name
# find files with exactly 664 permissions, only file with permissions 664 will be showed
find –perm 664
find –perm u=rw,g=rw,o=r
# find files with at least 664 permissions, E.g. 775 match as valid.
find –perm -664
find –perm –u=rw,g=rw,o=r
# find all files the owner can execute at least but all the rest can be anything
find -perm -100
find -perm -u=x
# find files with any of these permissions: "user has rw" or "group has rw" or "other has read perm"
find -perm /664
find –perm /u=rw,g=rw,o=r
# Find all files with at least permission write for group
find . -perm -g=w
#Find all SUID set files
find / -perm /u=s
# Find all SGID set files
find / -perm /g=s
# Find all the SGID bit files whose permissions are set to 644
find / -perm 2644
# Find all the Sticky Bit set files whose permission is 551
find / -perm 1551
# Find files that the group can write to, but others cannot read or write to it
find /var/log/ -perm -g=w ! -perm /o=rw
# Find a file that is exactly 213 kilobytes or a file that has permission 402 in octal
find /home/bob -size 213k -o -perm 402