replace all files with same name - macos

I have a file in multiple folder called PFSound.js
I've updated it so I want to replace all PFSound.js files in those directories for the new one,
is there a way to do it in just one time?
Mac or windows is ok
Thanks!!

On Mac/Linux, you can do this:
find . -type f -name "PFSound.js" -exec cp path/to/new/PFSound.js {} \;
assuming you wish to do that from the current directory downwards and that the new PFSound.js is located somewhere else.
That says.... find, starting at dot (the current directory) all things of type "file" with the name "PFSound.js", and for each one you find, execute the copy command and copy the new PFSound.js into the same place you just found an old one.

you can use a python script for this .
import os
newfilename="the_new_name"
def renamethefile(folderPath):
for fileOrFolder in os.listdir(folderPath):
if os.path.isdir(fileOrFolder) :
renamethefile(fileOrFolder)
continue
else:
os.rename(fileOrFolder, newfilename)
renamethefile("/path/to/the/folder");
i hope this help you .

Related

How can I delete all files in all subdirectories with a certain name?

I have been trying to use the command line to delete all files in all subdirectories with the name s_1_1102_c.jpg.
This question is similar to what I need How to remove folders with a certain name but it is removing directories and I only want to delete the files with the name s_1_1102_c.jpg.
I will need to remove this file from 260 subdirectories under the L001 directory. My directory structure is like this:
L001
C5.1
s_1_1101_a.jpg
s_1_1101_c.jpg
s_1_1101_g.jpg
s_1_1101_t.jpg
s_1_1102_a.jpg
s_1_1102_c.jpg
s_1_1102_g.jpg
s_1_1102_t.jpg
s_1_1103_a.jpg
s_1_1103_c.jpg
s_1_1103_g.jpg
s_1_1103_t.jpg
C6.1
s_1_1101_a.jpg
s_1_1101_c.jpg
s_1_1101_g.jpg
s_1_1101_t.jpg
s_1_1102_a.jpg
s_1_1102_c.jpg
s_1_1102_g.jpg
s_1_1102_t.jpg
s_1_1103_a.jpg
s_1_1103_c.jpg
s_1_1103_g.jpg
s_1_1103_t.jpg
Ultimately I need to remove several files from all subdirectories (s_1_1101_g.jpg, s_1_1101_t.jpg, s_1_1102_a.jpg, s_1_1102_c.jpg, s_1_1102_g.jpg, s_1_1102_t.jpg). So maybe there is a way to provide a list of the file names I need to delete.
How can I delete these files?
find . -name "s_1_1102_c.jpg" -exec rm -f {} \;
Note: This will find and delete the file in any subdirectory of the current one. So you could execute it in L001 or wherever else you want to do this.
for i in s_1_1101_g.jpg s_1_1101_t.jpg s_1_1102_a.jpg s_1_1102_c.jpg s_1_1102_g.jpg s_1_1102_t.jpg; do
echo rm L001/*/"$i";
done
If output looks fine, remove echo.
The final method I used to delete my files was given by #Peter - Reinstate Monica
for f in s_1_1101_t.jpg s_1_1102_a.jpg s_1_1102_c.jpg s_1_1102_g.jpg s_1_1102_t.jpg s_1_1103_a.jpg s_1_1103_c.jpg s_1_1103_g.jpg s_1_1103_t.jpg s_1_1104_a.jpg s_1_1104_c.jpg s_1_1104_g.jpg s_1_1104_t.jpg s_1_2101_g.jpg s_1_2101_t.jpg s_1_2102_a.jpg s_1_2102_c.jpg s_1_2102_g.jpg s_1_2102_t.jpg s_1_2103_a.jpg s_1_2103_c.jpg s_1_2103_g.jpg s_1_2103_t.jpg s_1_2104_g.jpg s_1_2104_t.jpg; do find /hpc/home/L001 -name $f -delete; done
I was concerned that my file list would be too long but it worked in this situation.

Recursively trying to rename files in Mac OS terminal

I have a many subfolders with files containing the text mainLine in them. I want to strip that text and rename all files recursively.
For example I'm trying to rename log12 mainLine.txt to log12.txt, I'm trying the following code:
find . -exec rename -nvs '* mainLine*' '' * {} +
But I'm getting files which contain that pattern as unchanged.
There are many versions of rename, and to my knowledge none of them are installed by default in Mac OS; but the options you used suggest you are using one which expects file names to rename as the third and subsequent arguments. You apparently copy/pasted it from somewhere without understanding it. You should replace the * with the files you want to rename, or better yet, run it once per directory with the wildcard intact.
find . -type d -execdir sh -c "rename -nvs '* mainLine*' '' *" \;

Find a file in Cygwin

I use find -name "nametofind" in cygwin to search for a file, but it does not give me any result, even when the file I want to search exists in the current directory. What am I doing wrong? Thanks.
As the comment mentioned more succinctly, you need to tell find which directory you want to search. If you it is the current directory, you should use ..
find . -name "nametofind"
It appears that the OP was trying to either match a partial file name or a file name with a different case. As #devnull mentioned in his comment, the correct solution for either case is to use the following.
find . -iname '*nametofind*'

Bash script to find file older than X days, then subsequently delete it, and any files with the same base name?

I am trying to figure out a way to search a directory for a file older than 365 days. If it finds a match, I'd like it to both delete the file and locate any other files in the directory that have the same basename, and delete those as well.
File name examples: 12345.pdf (Search for) then delete, 12345_a.pdf, 12345_xyz.pdf (delete if exist).
Thanks! I am very new to BASH scripting, so patience is appreciated ;-))
I doubt this can be done cleanly in a single pass.
Your best bet is to use -mtime or a variant to collect names and then use another find command to delete files matching those names.
UPDATE
With respect to your comment, I mean something like:
# find basenames of old files
find .... -printf '%f\n' | sort -u > oldfiles
for file in ($<oldfiles); do find . -name $file -exec rm; done

Unix: file names and location to excel sheet

I am new to Unix system. I am not sure is this task possible or how ?
I have this directory structure
c: parent dir/ child dir/file1
c: parent dir/ child dir/file2
c: parent dir/ child dir/file3
.
.
I need to export all this files name and its full location(path) to an excel sheet.
Any hint/help would be appreciated.
thanks
You can export the list returned from find to a CSV file. To get a list of files only (not directories) in a specific folder, not recursively, you would do:
find `pwd` -maxdepth 1 -type f -print > files.csv
I do not know of a way to write to an excel sheet, as it is not a text file. The best approach would probably be to output your data to a file in a format such as CSV or just plain text and then copy and paste that into an excel sheet.
Example:
$] find `pwd` > myfile.txt
/absolute/path/to/file0
/absolute/path/to/file1
...
And copy and paste that to your excel sheet. Or you could parse the data in from find and insert a comma after each line. I'll leave that as homework for you... should be easy enough.
I don't have access to a Unix system right now, but from a console window try
cd "root of directory structure"
find . -print > myFiles.csv
Load the .csv into Excel then putter as needed.
You may want to read up on the find command - it's very useful.

Resources