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  or replace the line


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.


to replace old line with new line and copy the updated file to new file.

sed -e 's:opt/sw/abc.txt:opt/sa/tcb.txt:g' inputfile.txt > newfile.txt


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

12) To create empty files we can use touch command and similary we can use mkdir to create new directory

touch filename.txt

This will create an empty file using with name filename.txt. 

To create file with certain time stamp we can use the below command

touch -a -m -t 202410101530.06 filename.txt

To Copy the content to new files we can use the redirect arrow.

cat filename.txt  >> filename1.txt

This will copy file the context of filename.txt to filename1.txt

mkdir folder1

This will create a directory with folder1

13) To extract value from a column delimited file

awk -F "," '{print $1}' Filename.txt | sort -d | head 10

The above command will fetch first 10 records of first column from the file name.txt with descending order.

cat Filename.txt  | awk -F "|" '{print $10}' | sort -d | uniq

The above command will fecth unique records of 10th column in a delimited text having pipe as delimiter .

awk -F "~" '{sum+=$6} END { printf "sum of 5th field" }' Filename.txt 

The above command will give the sum of 6th column

with filename.txt


14. To list the files with specific text in a folder 


grep -Ril "search keyword"   "filepath"

with two arguments like search keyword and filepath we can find the keyword that is used in all files.

15. For listing the files in Unix, we can use the command

ls  -  ls will allow you to list the files and folder in a particualr permissions

For see the files full details like last modfied date , file owner , file groups -lrt can be used

ls -lrt or ls -ltr can used used tol files with file owner ,file group and last modified stamp

For listing the hidden files in Unix the below command -a can used be along with -lrt

ls -lart  - This will all files inlcude hidden files in a directory

To see what are the folders that has extened permissions aprart from file owner and file groups below command can be used.

ls -le  - This will display the directory that has extened permission.

ls -le | grep +

After identifying the extened permission, For updating the group or add extra group

acledit an be used.


acledit directory


head :  used to for get top rows in a file

usage :  head -10 file.txt

The above command will fetch first 10 rows from a file file.txt

tail :  used to for get last rows in a file

usage :  tail -10 file.txt

The above command will fetch last 10 rows from a file file.txt


To check the number of lines wc command will be used

wc -l filename.txt  (filename.txt is the filename we need to check the number of lines)

To change the directory we use the cd command

cd /opt/sw/

/opt/sw/ is the directory

To copy the files we use the cp command

cp file1 file2

This will copy file1 to file2 using the same directory
we can specify full path if we need to copy to different directory.

pwd : This will display the present directory

more :  display data in read only format with each page. to move to next page we need enter.
ln : Create a soft link on oldname
file :  display the file type.
	
cut : will display certain charater from the file or text
echo "This is test" | cut -c 1-3,6-8
This will display This 

No comments: