Difference between revisions of "File Commands"

From rbachwiki
Jump to navigation Jump to search
 
(18 intermediate revisions by the same user not shown)
Line 15: Line 15:
== Find Directory sizes ==
== Find Directory sizes ==
  du -h --max-depth=2 | sort -hr
  du -h --max-depth=2 | sort -hr
[[Category:Bash]]
== Search multiple files for a keyword, then list the file names ==
find . -type f -name "*.php" -print0 | xargs -I {} -0 grep -r -l "edcas" "{}"
 
== Find files in multiple directories ==
''' use awk to clean up dir names then send to text files pass / as a delimiter to awk '''
find . -type f -name *.jpg | awk -F / {'print $3'} > file.txt


== Find files and rsync them to a dir ==
== Find files and rsync them to a dir ==
  find . -type f -name '*.jpg' -exec rsync -zavhP {} /dirctorytocopyto/ \;
  find . -type f -name '*.jpg' -exec rsync -zavhP {} /dirtocopyto/ \;


== Find Multiple filenames ==
== Find Multiple filenames ==
  find . -type f \( -name "*.nef" -o -name "*.jpg" -o -name "*.tif" \)
  find . -type f \( -name "*.nef" -o -name "*.jpg" -o -name "*.tif" \)
== Find Files with Specific extension and specific name ==
find . -type f -name '*top*' -and -name '*.jpg'
# will find files with *top* and with the extension .jpg '''
#use -iname for case insensitive


== RSYNC copy directories with spaces ==
== RSYNC copy directories with spaces ==
Line 33: Line 43:


== Find and delete files with certain names==
== Find and delete files with certain names==
  find -type -f -name '*one*' -exec rm {} \;
  find -type f -name '*one*' -exec rm {} \;
 
== Find specific files and copy them to another folder ==
== Find specific files and copy them to another folder ==
  find -type f -name "*app1*" -exec cp {} /var/www/test/ \;
  find -type f -name "*app1*" -exec cp {} /var/www/test/ \;
Line 71: Line 82:
  rename -n 's/.html/.php/' *.html
  rename -n 's/.html/.php/' *.html
=== Rename files adding sequential numbers Mac Only===
=== Rename files adding sequential numbers Mac Only===
  rename -N 03000 's/find/$N/gi' *.jpg
  rename -N 03000 's/searchterm/$N-out.jpg/gi' *.jpg
'' You have to add the 0 at the beginning otherwise it wont work''
'' You have to add the 0 at the beginning otherwise it wont work''
===Rename All files with sequential numbers Mac Only ===
rename -N 0001 's/.*/$N-bldg.jpg/' *.jpg
===Rename all Files with sequential numbers Linux ===
ls -t *.txt | cat -n | while read n f; do mv "$f" "$(printf bldg-%03d.txt $n)"; done
# %03d is the amount of digits
===Rename all Files with sequential numbers Linux bash file===
count=0;
for file in *.txt; do
  #check if the file is a regular file and not a directory
  if [ -f "$file" ]; then
    ((count++))
    #generate the new filename
    newname="$(printf bldg-%03d.txt $count)";
    mv "$file" "$newname"
  fi
done


=== Rename Delete string Mac Only===
=== Rename Delete string Mac Only===
Line 85: Line 115:
===Rename Prepend Mac Only ===
===Rename Prepend Mac Only ===
  rename -A 'xxxx-' *.jpg
  rename -A 'xxxx-' *.jpg
=== SCP Command ===
'''Copy file from a remote host to local host SCP example:'''
$ scp username@from_host:file.txt /local/directory/
'''Copy file from local host to a remote host SCP example:'''
$ scp file.txt username@to_host:/remote/directory/
'''Copy directory from a remote host to local host SCP example:'''
$ scp -r username@from_host:/remote/directory/  /local/directory/
'''Copy directory from local host to a remote hos SCP example:'''
$ scp -r /local/directory/ username@to_host:/remote/directory/
'''Copy file from remote host to remote host SCP example:'''
$ scp username@from_host:/remote/directory/file.txt username@to_host:/remote/directory/
''' SCP using key'''
scp -i ~/.ssh/id_rsa.pub FILENAME USER@SERVER:/home/USER/FILENAME
'''SCP options:'''
*–r Recursively copy entire directories. Note that this follows symbolic links encountered in the tree traversal.
*-C Compression enable. Passes the -C flag to ssh to enable compression.
*-i file Specify the file from which to read the identity for public key authentication.
*-l limit – Limits the used bandwidth, specified in Kbit/s.
*-o ssh_option – Can be used to pass options to ssh in the format used in ssh_config.
*-P port – Specifies the port to connect to on the remote host. Note that this option is written with a capital ‘P.’
*-p Preserves modification times, access times, and modes from the original file.
*-q Quiet mode: disables the progress meter as well as warning and diagnostic messages from ssh.
*-v Verbose mode. Print debugging messages about progress. This is helpful in debugging connection, authentication, and configuration problems.
<div class="keywords">scp with key</div>
[[Category:Bash]]

Latest revision as of 19:34, 6 February 2024

Find files x days old and delete them

#!/bin/bash
find /path/to/files/ -type f -name '*.jpg' -mtime +10 -exec mv {} /path/to/archive/ \;
find /path/to/archive/ -type f -name '*.jpg' -mtime +30 -exec rm {} \;

Find files over a certain size

$ find . -type f -size -4G

You can use size switch for other formats, such as

  • `c’ for bytes
  • ‘w’ for two-byte words
  • `k’ for Kilobytes
  • `M’ for Megabytes
  • `G’ for Gigabytes

Find Directory sizes

du -h --max-depth=2 | sort -hr

Search multiple files for a keyword, then list the file names

find . -type f -name "*.php" -print0 | xargs -I {} -0 grep -r -l "edcas" "{}"

Find files in multiple directories

use awk to clean up dir names then send to text files pass / as a delimiter to awk

find . -type f -name *.jpg | awk -F / {'print $3'} > file.txt

Find files and rsync them to a dir

find . -type f -name '*.jpg' -exec rsync -zavhP {} /dirtocopyto/ \;

Find Multiple filenames

find . -type f \( -name "*.nef" -o -name "*.jpg" -o -name "*.tif" \)

Find Files with Specific extension and specific name

find . -type f -name '*top*' -and -name '*.jpg'
# will find files with *top* and with the extension .jpg 
#use -iname for case insensitive

RSYNC copy directories with spaces

find . -type f -name '*177*' -exec rsync -zavP {} "/var/tmp/my dir" \;

Count Files in Directory

cd /var/www/

count=`ls-l *.sh | wc -l 2> /dev/null`
if [ $count -ge 1 ]; then
mv *.sql /var/www/backups/dirname
fi

Find and delete files with certain names

find -type f -name '*one*' -exec rm {} \;

Find specific files and copy them to another folder

find -type f -name "*app1*" -exec cp {} /var/www/test/ \;

Rename files adding the creation date as prefix

Get the date from the file

date -r $(stat -f %B filename.jpg) +%Y-%m-%d

Read exif data from file

mdls filename.jpg

find creation date of file in exif data

mdls filename | grep -i kMDItemContentCreationDate -m 1 | awk '{print $3}'
  1. the m1 will return the 1 result
exiftool filename.jgp

Using rename command

If its not installed in MacOS, use brew to install it

rename 's/dem/rob/g' * jpg

Will change dem and replace it with rob /g is global which will replace all occurrence of dem in the file name

rename 's/\+/_/g' *.tif

use the \ escape character with the + sign

Replace multiple characters

rename 's/[_,-,a,b,c,]//g'
rename '/.jpg/.tif/' *.jpg

Change jpg extension to tif

Deleting part of a filename

rename 's/dem//' *.jpg

Searching with Groupings

rename 's/(dem|rob)ng/bac/' *.jpg

The central expression of this rename command will search for strings within filenames that have the character sequence “dem” or “rob” where those sequences are immediately followed by “ng”. In other words, our search term is going to look for “string” and “demng”. The substitution term is “bacng”.

Example: demng.jpg,  robmg.jpg

Changing case of filename

rename 'y/a-z/A-Z/' *.jpg

Use the -n option to print names without renaming them

rename -n 's/.html/.php/' *.html

Rename files adding sequential numbers Mac Only

rename -N 03000 's/searchterm/$N-out.jpg/gi' *.jpg

You have to add the 0 at the beginning otherwise it wont work

Rename All files with sequential numbers Mac Only

rename -N 0001 's/.*/$N-bldg.jpg/' *.jpg

Rename all Files with sequential numbers Linux

ls -t *.txt | cat -n | while read n f; do mv "$f" "$(printf bldg-%03d.txt $n)"; done
# %03d is the amount of digits

Rename all Files with sequential numbers Linux bash file

count=0;
for file in *.txt; do
 #check if the file is a regular file and not a directory
 if [ -f "$file" ]; then
   ((count++))
   #generate the new filename
   newname="$(printf bldg-%03d.txt $count)";
   mv "$file" "$newname"
 fi
done

Rename Delete string Mac Only

rename -d '0' *.jpg

Result

030020-abc.jpg
30020-abc.jpg

Find and Rename

find . -type f -name '*2019*' -print0 | rename -0 's/2019/2020/gi'

Rename Prepend Mac Only

rename -A 'xxxx-' *.jpg

SCP Command

Copy file from a remote host to local host SCP example:

$ scp username@from_host:file.txt /local/directory/

Copy file from local host to a remote host SCP example:

$ scp file.txt username@to_host:/remote/directory/

Copy directory from a remote host to local host SCP example:

$ scp -r username@from_host:/remote/directory/  /local/directory/

Copy directory from local host to a remote hos SCP example:

$ scp -r /local/directory/ username@to_host:/remote/directory/

Copy file from remote host to remote host SCP example:

$ scp username@from_host:/remote/directory/file.txt username@to_host:/remote/directory/

SCP using key

scp -i ~/.ssh/id_rsa.pub FILENAME USER@SERVER:/home/USER/FILENAME


SCP options:

  • –r Recursively copy entire directories. Note that this follows symbolic links encountered in the tree traversal.
  • -C Compression enable. Passes the -C flag to ssh to enable compression.
  • -i file Specify the file from which to read the identity for public key authentication.
  • -l limit – Limits the used bandwidth, specified in Kbit/s.
  • -o ssh_option – Can be used to pass options to ssh in the format used in ssh_config.
  • -P port – Specifies the port to connect to on the remote host. Note that this option is written with a capital ‘P.’
  • -p Preserves modification times, access times, and modes from the original file.
  • -q Quiet mode: disables the progress meter as well as warning and diagnostic messages from ssh.
  • -v Verbose mode. Print debugging messages about progress. This is helpful in debugging connection, authentication, and configuration problems.
scp with key