Wednesday, June 16, 2021

UNIX File Operations and User permission Commands

 
1) Vi editor

ESC + ! + wq   -- This for saving the file
ESC + ! + q  --This will abort current changes
CNTRL +Z  also works for aborting the changes but it is not proper way to abort.
shift + G  -- Will come to the end of the file
dd -- typing two times in vi editor will delete the enitre line.
d -- only one time d will delete the one letter
i or insert button will be used for insert option. By default vi editory will be in edit mode. 
vi -R file.txt -- Opens the file.txt in read only mode


2) Change file group of a folder 

chgrp usergroup foldername
chgrp  -- Command name
usergroup -- user group that will be changed to
foldername -- folder details that need to be changed


3) to check the permission of a user

id userid
id -- Command
userid- any user login that need to check for their permission


4) to check the default group of user

id -Gn userid
id -- Command
Gn -- Group name
userid- any user login that need to check for their permissions

5) to check the current user groups

groups  -- command will be used to check current user groups

6) to check the full user details from passwd file.

cat /etc/passwd | cut -d: -f5
The above command gives all user details in Unix server.

6) to check the user full information finger command can be used

finger -- Command
finger username  -- This gives infromation about current user login name and home path and from which servers its connected.

7) to change the permission of file

chmod 775 filename
There are three 3 modes of permissions 

8) find command will be used to find the file in a folder or directory

find /var -xdev -type f -size +300000c -exec ls -al {} \; | sort -k5nr | head -50
The above command fetches the files from the mount point /var with files greater that 300000kb that included hidden files and then sort the files to retreive the 50 large files. 
-type :  There can be f (file) or d (directory)
ls :  for listing files 
sort :  for sorting the order
head:  for limiting the record after sorting
find command will be used to find the file in a folder or directory
find /var  -type f -name bkp* -exec ls -al {} \; | sort -k5nre | head -50
The above command fetches first 50 the files that starts with bkp name
Similary find can used with most of the file operation commands 
find /var -type f -name bkp* -exec chmod 775 {} \;
The above command changes permission of the all the files starts with bkp to rwxrwxr-x (775) .
find /var -type f -name bkp* -exec chmod 770 {} \;
The above command changes permission of the all the directories starts with bkp to rwxrwx--- (770) .
If the current user who executing the above command does not have permission then it will not change and will get permission denied error.

9) Removing the files 

For Removing the files we can use rm command
rm file.txt
Above command will remove the file.txt. For removing directory we can 
use rmdir command
rmdir testdir
Above command will will remove directory testdir. This will work only when directory is empty.
if we need to remove recursivly with subfolders and files we can use
rm -fR testdir
Above command will will remove directory testdir. This will remove the full directory. if there is any files inside directoy in which user does not have permission then it will skip that files and direcotry will not be removed.
find /var -type f -name bkp* -exec rm -fR {} \;
Above command will remove the files that starts with bkp in all directories. 
find /var -type f -name *.txt  -mtime +30  -exec rm -fR {} \;
Above command will remove the files that has txt extension which is older than 30 days from all the directories. 


10) Replace the keyword in a file


sed : sed command will be used for replacing the text
sed -e 's/orginal keyword/replacing keyword/g' inputfile.txt


example
-------- 
sed -e 's/abc/cda/g' inputfile.txt > newfile.txt
above command replace the text abc in the file inputfile.txt with cda and put into newfile.txt file.


11) moving the file from one place instead of copying


For moving the file we use the mv command. When you use mv command the actual file will be moved. The permissions and file creation timestamp will not be changed it will be as original file. 
mv  filename1.txt filename2.txt
In the above command it will rename filename1.txt to filename2.txt
mv /opt/sw/ss/filename1.txt /opt/sw/filename1.txt
The above command will move file from /opt/sw/ss/ drirectory to /opt/sw/ directory.

To move the entire directory we can use mvdir command

mvdir /opt/sw/ss/dir1 /opt/sw/dir1



No comments: