Useful Linux commands



File Manipulation


# How to copy a file? e.g., top-words-2.sh to top-words-3.sh 

cp -v top-words-2.sh top-words-3.sh
cp -v top-words-{2,3}.sh

# How to zip a folder into a .zip file? e.g., we want to create a .zip file called zipfilename.zip by zipping all files in the folder named folder_name

zip -r zipfilename.zip folder_name


Search


# How to search files containing a certain str?

grep -iRl "str to search" ./

-i - ignore text case
-R - recursively search files in subdirectories.
-l - show file names instead of file contents portions.


grep -rnw '/path/to/somewhere/' -e 'pattern'

# only search through those files which have .c or .h extensions

grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"

# exclude searching all the files ending with .o extension

grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"

# exclude a particular directory(ies) through --exclude-dir parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/

grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"




# today's file at current directory with certain regex

find . -maxdepth 1 -mtime 0 -name "*hello*"

# change multiple file names, e.g., *.txt files to *.text

for f in *.txt;
do mv -- "$f" "$(basename "$f" .txt).text"
done

# find and remove files

# first command remove directories as well
find . -name "FILE-TO-FIND" -exec rm -rf {} \;
# second command only removes files
find . -type f -name "FILE-TO-FIND" -exec rm -f {} \;

# how to delete all files with a certain extension in current directory & subdirectories?

check only:        find . -name "*.bak" -type f
delete as well:    find . -name "*.bak" -type f -delete

# how to delete files that are not belonging to a certain pattern?

find . -type f ! -name '*.txt' -delete

# search process id using certain port (e.g., 8000)

lsof -ti:8000
netstat -tulpn | grep ':443'

Others


# start default vino server in Ubuntu

/usr/lib/vino/vino-server

# clean trash bin

sudo apt install trash-cli
trash-empty

# get full permission to a file/folder
sudo chmod a+rwx /path/to/file
sudo chmod -R a+rwx /path/to/folder

# check status/start/stop/restart a service

sudo systemctl status [service name]
sudo systemctl start [service name]
sudo systemctl stop [service name]
sudo systemctl restart [service name]

No comments:

Post a Comment