File Permission Commands
The ls
command along with its -l
(for long listing) option will show you metadata about your Linux files, including the permissions set on the file.
$ ls -ltr
drwxr-xr-x 1 root root 0 Jan 29 23:04 dir1/
-rw-r--r-- 1 root root 0 Jan 29 23:06 file1
In this example, you see two different listings. The first field of the ls -ltr
output is a group of metadata that includes the permissions on each file. Here are the components :
User owner:
root
Group owner:
root
File type:
-
Permission settings:
rw-r--r--
rw- - The first set of permissions applies to the owner of the file.
r-- - The second set of permissions applies to the user group that owns the file.
r-- - The third set of permissions is generally referred to as "others."
For permissions, r
stands for read, w
for write, and x
for execute. For users, u
stands for user owner, g
for group owner, and o
for others.
chmod g+w filename - Add write permission to group
chmod g-wx filename - Remove write and execute permission from group
chmod o+w filename - Add write permission to others
chmod o-rwx foldername - Remove all permissions from others.
When Linux file permissions are represented by numbers, it's called numeric mode. A three-digit value represents specific file permissions, are called octal values.
r (read): 4
w (write): 2
x (execute): 1
chown <newUser> <fileName>
- changes the user ownership of a file.chown <newUser>:<newGroup> <fileName>
- changes the user & group ownerships of a file.chgrp <groupName> <fileName/dirName>
- updates the group name for file/directory.
GREP Command — Global Regular Expression Print
grep <text> <fileName>
- used to find text patterns within files.grep -i <text> <fileName>
- Used to find text patterns within the file ignoring the case (-i ignore case).grep -v <text> <fileName>
- Used to find non matching lines of text patterns (-v, --invert-match select non-matching lines).grep -l <text> <fileNames>
- Used to display the matching string file names. (-l, --files-with-matches print only names of FILEs containing matches)
Find Command
find . -name <fileName>
- find if the mentioned file is available in the current directory (.(period) represents current directory).find <dirName> -name <fileName>
- finds the mentioned file in the directory.
Disk and Memory usage by a file and directory
df -kh
- shows the disk space usage of mounted file systems.-h, --human-readable print sizes in powers of 1024 (e.g., 1023M)
-k like --block-size=1K
du -sh
- displays the total size of the directory instead of individual files in human-readable format.-h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
-s, --summarize display only a total for each argument
free -h
- shows systems memory information ( -h human readable format). The free command is a Linux command that allows you to check for memory RAM on your system or to check the memory statics of the Linux operating system.
Package manager in ubuntu
apt
- Advanced Packaging Tool - Package Manager for Debian-based Linux distributions Eg: Ubuntu.
apt
- a newer version of the package manager with colorized output, progress bar and additional functions.apt-get
- older version and basic package manager.
apt update
- Update the package index.
apt upgrade
- Upgrades the packages.
apt list --installed
- lists all the installed packages.
apt list --installed <package>
- shows the package name if it's installed.
apt show <package>
- shows information about a package mentioned.
apt search <package>
- searches and shows the list of packages.
apt install <package>
- installs the required package.
apt remove <package>
- removes the required package.
apt purge <package>
- removes the required package along with its config files.
Miscellaneous Commands
history
- To list all the commands run till now.
cut
- Cut command
cut -b 1,2 hello.txt
- prints only 1st and 2nd character from each line of the file (-b, --bytes=LIST select only these bytes)cut -b 1-3, 5-7 hello.txt
- prints only 1 to 3rd character and 5 to 7th character from each line of the file.cut -b 1- hello.txt
- In this, 1- indicate from 1st byte to end byte of a line.cut -b -3 hello.txt
- In this, -3 indicate from 1st byte to 3rd byte of a line.cut -c 1,2 hello.txt
- -c, --characters=LIST select only these characterscut -d "delimiter" -f (field number) file.txt
- It will separate the fields according the mentioned delimiter and prints the string/text in place of the field number.cat hello.txt
- Hello World!cut -d " " -f 1 hello.txt
- it will print "Hello"
sed
command in linux stands for stream editor
cat greet.txt
- Have a good day!
sed 's/good/great/' greet.txt
- Replaces good with great in greet.txt for the first occurrence.sed 's/good/great/2' greet.txt
- Replaces good with great in greet.txt for the second occurrencesed 's/good/great/g' greet.txt
- Replaces good with great in greet.txt for all the occurrences.sed 's/good/great/2g' greet.txt
- Replaces good with great in greet.txt for all the occurrences from the 2nd occurrence(except 1st occurrence).