file command + search directories and sub-directories - file

If file *.txt gets all text files from the current directory, how can I use it in order to get the text files from a specific directory and all its sub-directories?

maybe you are looking for
$ find . -name '*.txt' -exec file {} +

Related

How to move (mv) file by file starting from the last on macOS

I would like to move file by file from one folder to another starting with the last file and so on until there are no more files in the source folder.
I already managed to find and isolate the last file in the source folder with find and move to directory folder with mv:
find ~/Music/Music -not -path '*/\.*' -type f | gtac | head-1 | xargs -I '{}' mv {} ~/Music/iTunes/iTunes\ Media/Automatically\ Add\ to\ Music.localized
Is it also possible to add a rest time between the different mv?
Thank you in advance for your answers

Is there a way to move n files from n folders each folder contain 1 file with the same extension i.e (.pdf)

i have 126 folders in the same directory with names like this
"5d843c63-2043-499b-abd6-6ea0bbde5f58"
each folder contain 1 #pdf file
i want to move each file to one
i have used the command and managed to locate all the pdfs in the separated folders
locate ~/Desktop/wps\ /*pdf
then i used the " | " to move them at once but i couldn't i use the commnad
locate ~/Desktop/wps\ /*pdf | mv ~/Desktop/pdffff/
STOUD be like mv: missing destination file operand after '/home/yousef/Desktop/pdffff/'
using ubuntu
I don't know the locate command, but if it returns a list of paths of the files you want, do
mv `locate ~/Desktop/wps\ /*pdf` /path_to/new_folder_to_store_pdfs
The backticks ` ` will execute the locate command first, then the output of locate will be substituted into the outer mv command.
I did it just now with find and it worked.
mv `find -name *pdf` new_folder

find and rename multiple files on multiple folder

Finding a way to rename multiple files on a multiple folder
folder i.e. I have file called "jobsforms.html.bak" on a multiple folder under:
/home/sites/juk/jobsforms.html.bak
/home/sites/juan/jobsforms.html.bak
/home/sites/pedro/jobsforms.html.bak
/home/sites/luois/jobsforms.html.bak
I want to rename all the files found as: "jobsforms.html" how can I do that.
I was trying to do this aproach.
find /home/sites -name "jobsform.html.bak" -exec bash -c 'mv "$1" "${1%/*}"/jobsform.html' -- {} \;
Anyone can help me how to go about to do this.
Than you,
David
You could pipe the output of find to awk, using the sub function to remove a substring from the filename:
find /home/sites -name "jobsforms.html.bak" | awk '{ori=$0; sub(/\.bak$/,"",$0); system("mv \""ori"\" "$0)}'

Script to delete specific files from all zip archives in a directory

I'd like to delete the file with a name, say file.xml, from all zip archives in a given directory. Any simple solution is welcome, whether it's a bash script, python code or whatever.
Thanks!
Something like this would do it recursively in a dir
#!/bin/bash
find /my/dir -name "*.zip" -exec zip \{\} -d file.xml \;
Something along these lines:
find /starting/folder -name "*.zip"|xargs -I{} zip -d {} file.xml
Do make a backup of your zips before...

how to move files from one path to another path in unix using shell-script

i want to move logs files from one path(source path) to another path(destination path). This source path and destination path is present in one text file. as::
source_path=abc/xyz
source_path=pqr/abc
desination_path=abcd/mlk
so i read this file and stored paths in different variables.i have multiple source path so i stored it in a varible which acts as array. as below::
sourcePaths=`grep source_path myfile |cut -d= -f2`
destPath=`grep destination_path myfile |cut -d= -f2`
and i have only one destination path where i want to move all these files.
Now i want to travel through all these source path, find the logs file with specific extension, zip these all files and move to destination path.
i use loop to retrieve data from sourcePaths variable(this is just trial). as:;
for i in $sourcePaths
do
echo $i
done
Can anybody help me in this.
Thanks a lot for your reply Guys.Now i want to add some filter in this. I want to move files from Source path which are 10 days old,and i want to delete these files from source path after moving. Please help
Just one more question!! What if i want to store this zip file with name which contain time stamp,so that there will be no duplication of file name at destination path.
Hi All,one more question.When i am zipping the file with above option,it is creating subfolder in zip file according to its path.ie. if i am zipping logs files from src/int/xlog/abc, and when i unzip this zip file,these newly craeted zip file contents logs files in src/int/xlog/abc/abc.zip. i dont want this.what i want is that this zip file directly content abc.zip. Not in sub folder.
Hi Guys, i am back with one more question.Here using below script i am able to create filename of zip as base pathname.e.g.xyz.zip and abc.zip as per my above given path using below code::
timestamp=`date +%Y%m%d_%H%M%S`
zipFile=$destPath/$(basename $sourcePath).$timestamp.zip
But now i want to create this file name(i.e.Zip file name) more specific depends on its source path.Like if my text file contents
source_path=obj/imp/backup/logs/db1
source_path=obj/res/input/int/db2
desination_path=abcd/mlk
Now i want to create zip file name something like this.
backup_logs_db1.zip
input_int_db2.zip
means i want to use last three directory name to create new zip file name. It is provided that source path in text file contents more than three directory name.
So can anybody help me in this.
Thnaks in advance!!!
To create ZIP archives you can do this:
for sourcePath in $sourcePaths
do
zipFile=$destPath/$(basename $sourcePath).zip
find $sourcePath -type f -name "*.log" | xargs zip $zipFile -#
done
which will create
abcd/mlk/xyz.zip (containing all *.log files under abc/xyz) and
abcd/mlk/abc.zip (containing all *.log files under pqr/abc)
To create GZIPped archives you can:
for sourcePath in $sourcePaths
do
tarFile=$destPath/$(basename $sourcePath).tar
find $sourcePath -type f -name "*.log" -exec tar -uvf $tarFile {} \;
gzip $tarFile
done
which will create:
abcd/mlk/xyz.tar.gz (containing all *.log files under abc/xyz)
abcd/mlk/abc.tar.gz (containing all *.log files under pqr/abc)
To move files from source to dest, that have not been modified in the last 10 days, you can use this:
for sourcePath in $sourcePaths
do
find $sourcePath -type f -mtime +10 -name "*.log" -exec mv {} $destPath \;
done
If you want to delete the input files after zipping use the following:
for sourcePath in $sourcePaths
do
zipFile=$destPath/$(basename $sourcePath).zip
find $sourcePath -type f -mtime +10 -name "*.log" | xargs zip -mT $zipFile -#
done
Use the date command to generate a timestamp. The flags are:
%Y is the year
%m is the month
%d is the day
%H is the hour
%M are the minutes
%S are the seconds
Add the timestamp to the name of the zipFile, so it becomes:
for sourcePath in $sourcePaths
do
timestamp=`date +%Y%m%d_%H%M%S`
zipFile=$destPath/$(basename $sourcePath).$timestamp.zip
find $sourcePath -type f -mtime +10 -name "*.log" | xargs zip -mT $zipFile -#
done
Use the -j zip flag to store just the filename without the directory name. The new command will be:
for sourcePath in $sourcePaths
do
timestamp=`date +%Y%m%d_%H%M%S`
zipFile=$destPath/$(basename $sourcePath).$timestamp.zip
find $sourcePath -type f -mtime +10 -name "*.log" | xargs zip -jmT $zipFile -#
done
The following will iterate through all source paths, create a tar.gz file in $destPath. This .tar.gz contains a folder named $sourcePath containing logfiles (files with .log extension).
The name of the .tar.gz file will be based on the base name of $sourcePath, abc/xyz results in a file xyz.tar.gz to be created.
for sourcePath in $sourcePaths;do
name=`basename $sourcePath`
tar czf "$destPath/$name.tar.gz" $sourcePath/*.log
done
Note: your source_paths may not contain spaces or tabs, log files are not recursively searched.

Resources