nortmas
5/12/2014 - 8:00 AM

File System

# отобразить занятное пространство в dir-name
du -h /dir-name

#****** Work with archives ******#

#  разархивировать в текущую папку (-xvf - только лоя tar файлов)
tar -zxvf file.tar.gz                
#  разархивировать в текущую папку c заменой файлов
unzip -o file.zip                   
#  создание архива всех вайлов в текущей папке (sites/default/files/files.tar.gz)
tar -zcvf files.tar.gz ./            
#  создание архива папки folder_name
tar -zcvf files.tar.gz folder_name   

#****** Work with packages ******#

# Uninstall
sudo apt-get remove <package-name>
# Uninstall wkhtmltopdf and it's dependent packages
sudo apt-get remove --auto-remove <package-name>
# repe remove
sudo add-apt-repository --remove ppa:whatever/ppa
# Purging package
sudo apt-get purge 
# To delete configuration and/or data files and it's dependencies
sudo apt-get purge --auto-remove <package-name>
# Download 
wget <package-name>
# Install
sudo dpkg -i <package-name>.deb
# Install dependency
sudo apt-get -f install
# список установленых пакетов (с фельтрацией)
sudo dpkg -l | grep virtualbox       

#****** Create file ******#

#  создание файла с текстом
<?php echo 'Hello world'; ?> > foo.php
#  создание пустого файла
> foo.php
# create a file and put complex sting/variable inside
cat >> ~/.ssh/stage_private_key <<EOF
$SERVER_PRIVATE_KEY
EOF


#****** Search ******#

# Поиск файла
find . -name myFile.txt                 
# Поиск папки и удаление всех найденых
find . -name .git -exec rm -rf '{}' \;  
# поиск в файлайх включая внутренние каталоги
grep -rl 'string to find' path/where/find   

#****** System tree ******#

# hidden files also
ls -a                  
# Show only hidden files
ls -ld .?*  
# показать разрешения файла
ls -l filename

#  создание сиволической ссылки (sudo ln -s <from> <to>)
ln -s /etc/nginx/sites-available/site.loc /etc/nginx/sites-enabled/site.loc  


# Переименовать файл
mv file_name new_file_name              
# копировать пупку и все файлы
cp -avr /home/vivek/letters /usb/backup 


# поменять владельца файла
chown -R username filename              

# заменить разрешения файла
chmod 644 filename                      

777 - rwxrwxrwx
666 - rw-rw-rw-
555 - r-xr-xr-x
444 - r--r--r--

# ADD USER WITH ID 1003 AND ASSIGN IT TO THE GROUPS (www-data,mysql)
useradd -u 1003 -G www-data,mysql username
# CHANGE OWNER AND GROUP FOR FILE/DIR
sudo chown groupname:username filename
# ADD USER TO GROUP
sudo usermod -a -G groupname username
# REMOVE USER FROM GROUP
deluser username groupname
# SHOW EXISTING USER'S GROUPS or ALL GRUPS IF USER NAME IS NOT SPECIFIED
groups username
#show user IDs and groups
getent passwd username
#all users
less /etc/passwd

  
#****** REMOTE UPLOAD ******#
# download: remote -> local
scp user@remote_host:remote_file local_file   
# upload: local -> remote
scp local_file user@remote_host:remote_file      
# with port number and public key
scp -P 30033 -i ~/.ssh/id_rsa.pub <local_file_path> user@remote_host:remote_file   

# передача как аргументов в xargs и выполнение команды для всехпереданных аргументов.
# https://ru.wikipedia.org/wiki/Xargs

 # поиск файлов определенного типа, удалить все переданные аргументы.
find . -name "*.sh"| xargs rm -rf      

#  поиск в файлайх включая внутренние каталоги и замена
# Sed — это первичный потоковый редактор.
# sed [опции] команды [файл-для-редактирования]
grep -rl 'findString' path/to/file | xargs sed -i 's~findString~replaceString~g'  


# Поиск и удаление веток вида "feature/s9/67-link--dev" из ориджина с префиксом feature
git branch -r | awk -F/ '/\/feature/{print $2"/"$3"/"$4}' | xargs -I {} git push origin --delete {}  
# or remove only s12 branches
git branch -r | awk -F/ '/\/feature\/s12/{print $2"/s12/"$4}' | xargs -I {} git push origin --delete {}


# Удалить локально
git branch -a | grep feature/ | xargs git branch -d
# fetch the version of the module rdf
drush pmi rdf | grep Version | awk '{print $3}'
# show output starting from the line 2
ls -al | awk 'NR>2'
# show output line 2
ls -al | awk 'NR==2'
find . -maxdepth 3 -type f ! -perm u=rw,g=r,o=r | egrep -v "config" | egrep -v "vendor"
find . -type f ! -perm u=rw,g=r,o=r \( ! -path "*/config/*" ! -path "*/vendor/*" ! -path "*/files/*" ! -path "*/node_modules/*" \)
find . -type f ! -perm u=rw,g=r,o=r | grep -vFf skip_files
find . -type f ! -perm u=rw,g=r,o=r $(printf "! -name %s " $(cat skip_files))
# The standard output stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, it gets overwritten.
command > output.txt

# The standard output stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.
command >> output.txt

# The standard error stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, it gets overwritten.
command 2> output.txt

# The standard error stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.
command 2>> output.txt

# Both the standard output and standard error stream will be redirected to the file only, nothing will be visible in the terminal. If the file already exists, it gets overwritten.
command &> output.txt

# Both the standard output and standard error stream will be redirected to the file only, nothing will be visible in the terminal. If the file already exists, the new data will get appended to the end of the file..
command &>> output.txt

# The standard output stream will be copied to the file, it will still be visible in the terminal. If the file already exists, it gets overwritten.
command | tee output.txt

# The standard output stream will be copied to the file, it will still be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.
# (*)
# Bash has no shorthand syntax that allows piping only StdErr to a second command, which would be needed here in combination with tee again to complete the table. If you really need something like that, please look at "How to pipe stderr, and not stdout?" on Stack Overflow for some ways how this can be done e.g. by swapping streams or using process substitution.
command | tee -a output.txt

# Both the standard output and standard error streams will be copied to the file while still being visible in the terminal. If the file already exists, it gets overwritten.
command |& tee output.txt

# Both the standard output and standard error streams will be copied to the file while still being visible in the terminal. If the file already exists, the new data will get appended to the end of the file.
command |& tee -a output.txt