Bash: Find, copy and rename files - bash

I am trying to copy files (on the same server) created in the last 24 hours using the filename (Unix timestamp):
find /srv/docs/files/ -type f -iname '*.pdf' -mtime -1 -exec cp {} /srv/docs/pdf \;
However, I would like to extend this further. I would like to accomplish the following:
All files are named in this manner 1425787200.pdf. I would like to rename the file Sunday-0400.pdf
I would would like to create a new directory /srv/docs/pdf/day/time/ .
A file named 1425787200.pdf would end up in /srv/docs/pdf/Sunday/0400/Sunday-0400.pdf
Additionally, if the file already exists, an incremental number should be added to the file. eg Sunday-0400-1.pdf, Sunday-0400-2.pdf
Any help would be appreciated.

To change file name 1425787200.pdf to /srv/docs/pdf/Sunday/0400/Sunday-0400.pdf use something like this:
#!/usr/bin/bash
file=1425787200.pdf
day=$(date +"%A" -d "#${file%.*}")
time=$(date +"%H%M" -d "#${file%.*}")
new_file=/srv/docs/pdf/$day/$time/$day-$time.pdf
if [ -e "$new_file" ]; then
num_file=${new_file%.*}
num=1
while [ -e "${num_file}-${num}.pdf" ]; do
num=$(( num + 1 ))
done
new_file=${num_file}-${num}.pdf
fi
mv $file $new_file
this will also number duplcates.

Related

How to rename a group of similar files with only part of file name?

I have a group of files:
Ursus_fibroblast_ACT_34_CT0_adak_HJ_EV_S45_L002_R1_001_val_1.fq.gzAligned.out.sam
Ursus_fibroblast_ACT_34_CT12_adak_HJ_EV_S49_L002_R1_001_val_1.fq.gzAligned.out.sam
Ursus_fibroblast_ACT_34_CT0_adak_HJ_EV_S45_L002_R1_001_val_1.fq.gzAligned.out.sam
Ursus_fibroblast_ACT_34_CT12_adak_HJ_EV_S49_L002_R1_001_val_1.fq.gzAligned.out.sam
Ursus_fibroblast_ACT_34_CT15_adak_HJ_EV_S50_L002_R1_001_val_1.fq.gzAligned.out.sam
I would like to rename them with only this part of the name plus the extension "Aligned.out.sam":
"ACT_34_CT15_adak_HJ_EV_S50" "ACT_34_CT12_adak_HJ_EV_S49" and so on.
I have a start of a script so far but I am unsure of what to do now....I am obviously new to coding so if you need more information please let me know.
files={*.sam}
for i in $files
echo $i
mv -i $i
If your files are in the same directory this command will do the job.
for x in *.sam; do mv $x ${x%.sam}.Aligned.out.sam;done
while read file
do
target=${file//Ursus_fibroblast_}
target=${target//_L002*}
echo mv "$file" "$target.Aligned.out.sam" # this is an echo
done < <(find . -type f -name "*.sam")
mv ./Ursus_fibroblast_ACT_34_CT0_adak_HJ_EV_S45_L002_R1_001_val_1.fq.gzAligned.out.sam ./ACT_34_CT0_adak_HJ_EV_S45.Aligned.out.sam
mv ./Ursus_fibroblast_ACT_34_CT12_adak_HJ_EV_S49_L002_R1_001_val_1.fq.gzAligned.out.sam ./ACT_34_CT12_adak_HJ_EV_S49.Aligned.out.sam
mv ./Ursus_fibroblast_ACT_34_CT15_adak_HJ_EV_S50_L002_R1_001_val_1.fq.gzAligned.out.sam ./ACT_34_CT15_adak_HJ_EV_S50.Aligned.out.sam

How to print the number of locations of file found

Prompt the user for a file name, without the path (Ex: xyz.out)
- Using the find command, provide the full path to the file
- At the end, “print number of locations of that file found”
- If it’s not found, then display “not found
and this is my script
#! /bin /bash
echo "please enter your file name"
read filename
if [ -f $filename ];
then
echo "file $filename found"
find $PWD -type f | grep $filename
#find "$(cd ..; pwd)" -name $filename
else
echo "file $filename was not found"
fi
but the thing is At the end, i need to “print number of locations of that file found”
help me out with this
Something like this to get the count:
find $PWD -type f -name $filename 2>/dev/null | wc -l
This should work:
find "$PWD" -type f -name "$fname" |grep "" -c
In trying to keep it as short as possible, one approach with Posix shell would be to fill a temporary file with the file names returned by find, cat the file to provide your output, and use wc to provide the line count (note: you use your own pattern instead of "*cmpf*" shown below):
$ find . -name "*cmpf*" -printf "%P\n" >tmp; cat tmp; c=$(wc -l <tmp); \
rm tmp; printf "[%s] files found\n" $c
cmpf1f2.c
cmpf1f2_2.c
bin/cmpf1f2_2
bin/cmpf1f2
snip/cmpf1f2_notes.txt
cmpf1f2_for.c
[6] files found
If bash is available, another approach is to read the matching files into an array and then use the number of elements as your count. Example:
$ a=( $(find . -name "*cmpf*" -printf "%P\n") ); printf "%s\n" ${a[#]}; \
printf -- "--\n'%s' files found.\n" ${#a[#]}
cmpf1f2.c
cmpf1f2_2.c
bin/cmpf1f2_2
bin/cmpf1f2
snip/cmpf1f2_notes.txt
cmpf1f2_for.c
--
'6' files found.
Both approaches give you both the files and directories in which they reside as well as the count of the files returned.
Note: if you would like ./ before each file and directory names, use the %p format instead of %P above.

how to remove all the normal png in the xcode project

I want to remove all the images which has the name without the "#2x", and I want to write a shell script to finish this. This is what I do:
#!/bin/bash
dir="/Users/me/Workspace/"
cd $dir
all_pngs=`find . -name "*.png" | sort -u`
for png in $all_pngs
do
# echo "$png"
#get the dirname
dirname=`dirname $png`
#get the filename without dir
filename=`basename $png`
#get name without suffix
name=`echo "$filename" | cut -d '.' -f1`
realname=`echo "$name" | grep -v "#2x"`
if [ -n $realname ]; then
echo "$realname"
fi
done
My problem is that I don't know how can I find the name without the "#2x".
I'm not really sure what you're trying to do with the rest of your script, but just something like this should work
find /Users/me/Workspace/ -type f -name '*.png' \! -name '*#2x*' -exec echo rm '{}' +
Remove the echo when you're confident that's what you want.
Since the ! exp has a higher precedence in find than the implied -a between tests and actions the above gets treated as
find /Users/me/Workspace/ (-type f) AND (-name '*.png') AND (! -name '*#2x*') AND (-exec echo rm '{}' +)
You have used many unwanted operation in your for loop which are not necessary( but exactly any purpose of it?). You need simple logic in your for..loop as below. OR in one sentence you can use nice answer given by #BroSlow
You can check if your file name contain "#2x" or not like
if [[ $png = *#2x* ]] //Yes it contain "#2x"
then
echo "File name contain #2x keep as it is."
else
//remove file // rm -f $png
fi
Using grep
if grep -o "#2x" <<<"$png" >/dev/null
then
echo "File name contain #2x keep as it is."
else
//remove file // rm -f $png
fi

bash delete directories based on contents

Currently I have multiple directories
Directory1 Directory2 Directory3 Directory4
each of these directories contain files (the files are somewhat cryptic)
what i wish to do is scan files within the folders to see if certain files are present, if they are then leave that folder alone, if the certain files are not present then just delete the entire directory. here is what i mean:
im searching for the files that have the word .pass. in the filename.
Say Directory 4 has that file that im looking for
Direcotry4:
file1.temp.pass.exmpl
file1.temp.exmpl
file1.tmp
and the rest of the Directories do not have that specific file:
file.temp
file.exmp
file.tmp.other
so i would like to delete Directory1,2 and3 But only keep Directory 4...
So far i have come up with this code
(arr is a array of all the directory names)
for x in ${arr[#]}
do
find $x -type f ! -name "*pass*" -exec rd {} $x\;
done
another way i have thought of doing this is like this:
for x in ${arr[#]}
do
cd $x find . -type f ! -name "*Pass*" | xargs -i rd {} $x/
done
SO far these don't seem to work, and im scared that i might do something wrong and have all my files deleted.....(i have backed up)
is there any way that i can do this? remember i want Directory 4 to be unchanged, everything in it i want to keep
To see if your directory contains a pass file:
if [ "" = "$(find directory -iname '*pass*' -type f | head -n 1)" ]
then
echo notfound
else
echo found
fi
To do that in a loop:
for x in "${arr[#]}"
do
if [ "" = "$(find "$x" -iname '*pass*' -type f | head -n 1)" ]
then
rm -rf "$x"
fi
done
Try this:
# arr is a array of all the directory names
for x in ${arr[#]}
do
ret=$(find "$x" -type f -name "*pass*" -exec echo "0" \;)
# expect zero length $ret value to remove directory
if [ -z "$ret" ]; then
# remove dir
rm -rf "$x"
fi
done

How to loop through a directory recursively to delete files with certain extensions

I need to loop through a directory recursively and remove all files with extension .pdf and .doc. I'm managing to loop through a directory recursively but not managing to filter the files with the above mentioned file extensions.
My code so far
#/bin/sh
SEARCH_FOLDER="/tmp/*"
for f in $SEARCH_FOLDER
do
if [ -d "$f" ]
then
for ff in $f/*
do
echo "Processing $ff"
done
else
echo "Processing file $f"
fi
done
I need help to complete the code, since I'm not getting anywhere.
As a followup to mouviciel's answer, you could also do this as a for loop, instead of using xargs. I often find xargs cumbersome, especially if I need to do something more complicated in each iteration.
for f in $(find /tmp -name '*.pdf' -or -name '*.doc'); do rm $f; done
As a number of people have commented, this will fail if there are spaces in filenames. You can work around this by temporarily setting the IFS (internal field seperator) to the newline character. This also fails if there are wildcard characters \[?* in the file names. You can work around that by temporarily disabling wildcard expansion (globbing).
IFS=$'\n'; set -f
for f in $(find /tmp -name '*.pdf' -or -name '*.doc'); do rm "$f"; done
unset IFS; set +f
If you have newlines in your filenames, then that won't work either. You're better off with an xargs based solution:
find /tmp \( -name '*.pdf' -or -name '*.doc' \) -print0 | xargs -0 rm
(The escaped brackets are required here to have the -print0 apply to both or clauses.)
GNU and *BSD find also has a -delete action, which would look like this:
find /tmp \( -name '*.pdf' -or -name '*.doc' \) -delete
find is just made for that.
find /tmp -name '*.pdf' -or -name '*.doc' | xargs rm
Without find:
for f in /tmp/* tmp/**/* ; do
...
done;
/tmp/* are files in dir and /tmp/**/* are files in subfolders. It is possible that you have to enable globstar option (shopt -s globstar).
So for the question the code should look like this:
shopt -s globstar
for f in /tmp/*.pdf /tmp/*.doc tmp/**/*.pdf tmp/**/*.doc ; do
rm "$f"
done
Note that this requires bash ≥4.0 (or zsh without shopt -s globstar, or ksh with set -o globstar instead of shopt -s globstar). Furthermore, in bash <4.3, this traverses symbolic links to directories as well as directories, which is usually not desirable.
If you want to do something recursively, I suggest you use recursion (yes, you can do it using stacks and so on, but hey).
recursiverm() {
for d in *; do
if [ -d "$d" ]; then
(cd -- "$d" && recursiverm)
fi
rm -f *.pdf
rm -f *.doc
done
}
(cd /tmp; recursiverm)
That said, find is probably a better choice as has already been suggested.
Here is an example using shell (bash):
#!/bin/bash
# loop & print a folder recusively,
print_folder_recurse() {
for i in "$1"/*;do
if [ -d "$i" ];then
echo "dir: $i"
print_folder_recurse "$i"
elif [ -f "$i" ]; then
echo "file: $i"
fi
done
}
# try get path from param
path=""
if [ -d "$1" ]; then
path=$1;
else
path="/tmp"
fi
echo "base path: $path"
print_folder_recurse $path
This doesn't answer your question directly, but you can solve your problem with a one-liner:
find /tmp \( -name "*.pdf" -o -name "*.doc" \) -type f -exec rm {} +
Some versions of find (GNU, BSD) have a -delete action which you can use instead of calling rm:
find /tmp \( -name "*.pdf" -o -name "*.doc" \) -type f -delete
For bash (since version 4.0):
shopt -s globstar nullglob dotglob
echo **/*".ext"
That's all.
The trailing extension ".ext" there to select files (or dirs) with that extension.
Option globstar activates the ** (search recursivelly).
Option nullglob removes an * when it matches no file/dir.
Option dotglob includes files that start wit a dot (hidden files).
Beware that before bash 4.3, **/ also traverses symbolic links to directories which is not desirable.
This method handles spaces well.
files="$(find -L "$dir" -type f)"
echo "Count: $(echo -n "$files" | wc -l)"
echo "$files" | while read file; do
echo "$file"
done
Edit, fixes off-by-one
function count() {
files="$(find -L "$1" -type f)";
if [[ "$files" == "" ]]; then
echo "No files";
return 0;
fi
file_count=$(echo "$files" | wc -l)
echo "Count: $file_count"
echo "$files" | while read file; do
echo "$file"
done
}
This is the simplest way I know to do this:
rm **/#(*.doc|*.pdf)
** makes this work recursively
#(*.doc|*.pdf) looks for a file ending in pdf OR doc
Easy to safely test by replacing rm with ls
The following function would recursively iterate through all the directories in the \home\ubuntu directory( whole directory structure under ubuntu ) and apply the necessary checks in else block.
function check {
for file in $1/*
do
if [ -d "$file" ]
then
check $file
else
##check for the file
if [ $(head -c 4 "$file") = "%PDF" ]; then
rm -r $file
fi
fi
done
}
domain=/home/ubuntu
check $domain
There is no reason to pipe the output of find into another utility. find has a -delete flag built into it.
find /tmp -name '*.pdf' -or -name '*.doc' -delete
The other answers provided will not include files or directories that start with a . the following worked for me:
#/bin/sh
getAll()
{
local fl1="$1"/*;
local fl2="$1"/.[!.]*;
local fl3="$1"/..?*;
for inpath in "$1"/* "$1"/.[!.]* "$1"/..?*; do
if [ "$inpath" != "$fl1" -a "$inpath" != "$fl2" -a "$inpath" != "$fl3" ]; then
stat --printf="%F\0%n\0\n" -- "$inpath";
if [ -d "$inpath" ]; then
getAll "$inpath"
#elif [ -f $inpath ]; then
fi;
fi;
done;
}
I think the most straightforward solution is to use recursion, in the following example, I have printed all the file names in the directory and its subdirectories.
You can modify it according to your needs.
#!/bin/bash
printAll() {
for i in "$1"/*;do # for all in the root
if [ -f "$i" ]; then # if a file exists
echo "$i" # print the file name
elif [ -d "$i" ];then # if a directroy exists
printAll "$i" # call printAll inside it (recursion)
fi
done
}
printAll $1 # e.g.: ./printAll.sh .
OUTPUT:
> ./printAll.sh .
./demoDir/4
./demoDir/mo st/1
./demoDir/m2/1557/5
./demoDir/Me/nna/7
./TEST
It works fine with spaces as well!
Note:
You can use echo $(basename "$i") # print the file name to print the file name without its path.
OR: Use echo ${i%/##*/}; # print the file name which runs extremely faster, without having to call the external basename.
Just do
find . -name '*.pdf'|xargs rm
If you can change the shell used to run the command, you can use ZSH to do the job.
#!/usr/bin/zsh
for file in /tmp/**/*
do
echo $file
done
This will recursively loop through all files/folders.
The following will loop through the given directory recursively and list all the contents :
for d in /home/ubuntu/*;
do
echo "listing contents of dir: $d";
ls -l $d/;
done

Resources