批量修改文件夹及文件权限

单独设置某个目录下所有文件夹的权限

find -type d -exec chmod 0755 {} \;

单独设置某个目录下所有文件的权限

find -not -type d -exec chmod 644 {} \;

如果要单独设置文件夹的所有者,只需要将命令改成如下即可:

find -type d -exec chown root.root {} \;

查找目录下的所有文件中是否含有某个字符串

find .| xargs grep -ri "class"

查找目录下所有文件,并且打印含有该字符串的文件名

find . | xargs grep -ri "class" -l

unzip Chinese gbk named files

unzip -O cp936 *.zip

批量解压windows下到文件

#!/bin/bash

for i in *.zip;
    do unzip -O cp936 "$i";
done

tar

pack all files in current directory

for file in `ls`; do tar -cvzf $file.tar.gz $file; done

Rename files/documents

delete space in file name

ls|while read i;do  
    mv "$i" $(echo $i|tr -d ' ') 2>/dev/null  
done 

Change space to ‘_’

for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done

Change first five bytes to ‘Github’

for i in `ls`; do mv -f $i `echo $i | sed 's/^...../Github/'`; done

change lower case to upper case

for i in `ls`; do mv -f $i `echo $i | tr a-z A-Z`; done

Suppose the ip address of your vps is 66.66.66.66, and your ssh port is 22222.

Then you can log in with

ssh centos@66.66.66.66 -p 22222

And you can download file with

scp -P 22222 centos@66.66.66.66:/home/centos/.vim.rc  ./

rename files

Rename all jpe file to jpg

rename 's/\.jpe$/\.jpg/' *.jpe

Screenrecord with the following command

avconv -f x11grab -r 25 -s 1920x1080 -i :0.0+0,0 -vcodec libx264  -threads 0 date.mkv

Statistics the number and type of files

I downloaded linux kernel 4.12.3 tarball, and found there are 64052 file in the tarball, now I want to know exactly how many kind of files there, how many files of each kind.

I run the following command:

tar -tf linux-4.12.3.tar.xz > linux-4.12.3-dir.tree
cat linux-4.12.3-dir.tree | grep -E -o '*\.[A-Za-z0-9]{1,19}$' | sort -n |uniq -c | sort -nr
Then I got this result, Press here to see 206 more lines

commands analysis

tar -tf linux-4.12.3.tar.xz //list the contents of an archive
cat linux-4.12.3-dir.tree   //concatenate files and print on the standard output
grep -E -o '*\.[A-Za-z0-9]{1,19}$'  //get the file type info
sort -n                             //sort lines of text files, compare according to string numerical value
uniq -c                             //report or omit repeated lines, prefix lines by the number of occurrences
sort -nr                            //sort lines of text files, compare according to string numerical value, reverse the result of comparisons

Reference

-Linux 批量修改文件夹、文件的权限和所有者 -Linux查找目录下的所有文件中是否含有某个字符串