Move and rename files based on the folder they are in - bash

I am trying to move some files in ANT but can't figure out how to do it. I know how to do it in a sequential way but can't figure out the "ant way" to do this.
Move the files from:
./<language>/<FileName>.properties
to:
./<FileName>_<language>.properties
So for example I have:
./fr/file1.properties
./fr/file2.properties
./fr/file3.properties
./en/file1.properties
./en/file2.properties
./en/file3.properties
./ko/file1.properties
./ko/file2.properties
./ko/file3.properties
I need to move these up one dir and rename the files like this:
./file1_fr.properties
./file2_fr.properties
./file3_fr.properties
./file1_en.properties
./file2_en.properties
./file3_en.properties
./file1_ko.properties
./file2_ko.properties
./file3_ko.properties
Is there an easy way to do this mapping in ant? I don't know what languages I will support or what the filenames may be.
In bash this would be straightforward. I would do something like this:
find ./* -maxdepth 0 -type d | while read DIR; do
# */ Correct syntax highlighting
find $DIR -maxdepth 0 -type f | while read FILE; do
# Note: this would produce file1.properties_fr
# which isn't exactly right. Probably need to
# use sed to remove and add .properties.
mv $DIR/$FILE ./$FILE_$DIR
done;
done;

Use a regular expression mapper in a move task:
<target name="rename">
<move todir=".">
<fileset dir=".">
<include name="**/*.properties" />
</fileset>
<mapper type="regexp" from="([^/]*)/([^/]*)(\.properties)$" to="\2_\1\3" />
</move>
</target>

Related

How to replace long one html-like string in files across multiple files in directories[sed,grep]?

I need to replace:
<m2: SplashScreen Image="Assets\SplashScreen.png"/></m2:VisualElements>
with this long string:
<m2:SplashScreen Image="Assets\SplashScreen.png" BackgroundColor="#ffffff" />
<m2:InitialRotationPreference>
<m2:Rotation Preference="landscape" />
</m2:InitialRotationPreference>
in many similar Package.appxmanifest file - not txt files. These file can be opened through texteditor
sed command did work, but only in one file.
sed -i '' 's/SplashScreen/xxxx/g' Package.appxmanifest
I want this replace done in all package.appxmanifest files across directories recursively.
attach to find
find yourpath -name Package.appxmanifest -exec sed -i ... {} \;
fill in the ... with your sed script.

Merging Multiple .mp3 files

On mac/linux there is a command to merge mp3 files together which is
cat file1.mp3 file2.mp3 > newfile.mp3
I was wondering if there is a simpler way or command to select multiple mp3's in a folder and output it as a single file?
The find command would work. In this example I produce a sorted list of *.mp3 files in the current directory, cat the file and append it to the output file called out
find . -maxdepth 1 -type f -name '*.mp3' -print0 |
sort -z |
xargs -0 cat -- >>out
I should warn you though. If your mp3 files have id3 headers in them then simply appending the files is not a good way to go because the headers are going to wind up littered in the file. There are some tools that manage this much better. http://mp3wrap.sourceforge.net/ for example.
simply linking files together won't work. Don't forget modern Mp3 files have metadata in the head. Even if you don't care about the player name, album name etc, you should at least make the "end of file" mark correct.
Better use some tools like http://mulholland.xyz/dev/mp3cat/.
You can use mp3cat by Darren Mulholland available at https://darrenmulholland.com/dev/mp3cat.html
Source is available at https://github.com/dmulholland/mp3cat

bash- use filename to append to every line in each file using sed

I have multiple files named as such --> 100.txt, 101.txt, 102.txt, etc.
The files are located within a directory. For every one of these files, I need to append the number before the extension in the file name to every line in the file.
So if the file content of 100.txt is:
blahblahblah
blahblahblah
...
I need the output to be:
blahblahblah 100
blahblahblah 100
...
I need to do this using sed.
My current code looks like this, but it is ugly and not very concise:
dir=$1
for file in $dir/*
do
base=$(basename $file)
filename="${base%.*}"
sed "s/$/ $filename/" $file
done
Is it possible to do this in such a way?
find $dir/* -exec sed ... {} \;
The code you already have is essentially the simplest, shortest way of performing the task in bash. The only changes I would make are to pass -i to sed, assuming you are using GNU sed (otherwise you will need to redirect the output to a temporary file, remove the old file, and move the new file into its place), and to provide a default value in case $1 is empty.
dir="${1:-.}"
the following command line will find all files that that has filename with only numbers with an extension and append the filename (numbers) at the end of each line in that file..(I tested with a couple of files)
find <directory path> -type f -name '[0-9]*' -exec bash -c 'num=`basename "{}"|sed "s/^\([0-9]\{1,\}\)\..*/\1/"`;sed -i.bak "s/.$/& $num/" "{}"' \;
Note: command line using sed not tested in OS X
replace <directory path> with the path of your directory

Finding Multiple Files, Copying, and Renaming Sequentially

I have a few hundred thousand files in many, many subdirectories. I am trying to extract all of the relevant image files, using a regular expression like so:
find -E . -regex '.+\.ca/.+(\.gif|\.jpg|\.tif|\.jpeg|\.tiff|\.png|\.jp2|\.j2k|\.bmp|\.pict|\.wmf|\.emf|\.ico|\.xbm)'
This finds the files. However, I want to move them to a newdir and have them named like so:
1.png
2.jpg
3.ico
4.pict
5.png
And so forth. I haven't been able to find a way that (a) preserves the various extensions; (b) renames them as they come in. Many of the files will be duplicates and I will want to preserve that. Thanks so much for your help.
i=1
find ... | while read filename; do
newname=$i.${filename##*.}
mv "$filename" newdir/"$newname"
i=$((i+1))
done

Bash script to transverse a directory

I have a directory with XML files and other directories. All other directories have XML files and subdirectories, etc.
I need to write a script (bash probably) that for each directory runs java XMLBeautifier directory and since my skills at bash scripting are a bit rubbish, I would really appreciate a bit of help.
If you have to get the directories, you can use:
$ find . -type d
just pipe this to your program like this:
$ find . -type d | xargs java XMLBeautifier
Another approach would be to get all the files with find and pipe that to your program like this:
$ find . -name "*.xml" | xargs java XMLBeautifier
This takes all .xml files from the current directory and recursively through all subdirectories. Then hands them one by one over with xargs to java XMLBeautifier.
Find is an awesome tool ... however, if you are not sure of the file name but have a vague idea of what those xml file contains then you can use grep.
For instance, if you know for sure that all your xml files contains a phrase "correct xml file" (you can change this phrase to what you feel appropriate) then run the following at your command line ...
grep -IRw "correct xml file" /path/to/directory/*
-I option searches the file and returns the file name when pattern is matched
-R option reaches your directory recursively
-w ensure that the pattern given matches on the whole and not single word individually
Hope this helps!

Resources