Create script taking output from another - bash

Basically I want to have a (generated) playlist in every folder containing music, sorted in order and has the same name as the folder.
I have a script that takes the information from music files and creates a playlist in a specific format.
1) I can run the script on single folder by
$ ./playlist.sh ~/Path/To/Music > output.xwaxpls
2) Put the playlist in a way that increases numerically (all my music is formatted %track% - %artist% - %title%)
$ sort -o output.xwaxpls output.xwaxpls
3) Change the name of output.xwaxpls to parent directory name
Unable to get this far yet.
Obviously I would love a script that would do all this.
I'm trying to learn and do each step one at a time but sadly I'm falling at the first hurdle. I can't even work out how to run the playlist.sh on all subdirectories. It's putting me off as I'm stuck before I can try the other two.
I would love some help....
Edit:
Another few hours and I'm getting somewhere !!
`#!/bin/sh
curdir=$(pwd)
for folder in ~/Music/*; do
[ -d "$folder" ] && cd "$folder" && ~/Desktop/./playlist.sh "$folder" > "$folder".xwaxpls && sort -o "$folder".xwaxpls "$folder".xwaxpls
done
`
This creates the playlists that I want, in the format that I want.......but the playlists are saved in ~/Music, not in the folder that contain the files.
I have tried to change the ~/Desktop/./playlist.sh "$folder" > "$folder".xwaxpls part to ~/Desktop/./playlist.sh "$folder" > "$folder"/"$folder".xwaxpls but an error is kicked out saying cannot create /home/user/Music/Singles//home/user/Music/Singles.xwaxpls: Directory nonexistent.
I have also tried to add && mv $playlist.xwaxpls $playlist/$playlist.xwaxpls which results in the same // error.
The // I understand, as I am forcing a double path but why is the xwaxpls file not written to the $folder in the first place ? I presume it's because I have specified the ~/Music/* in the for line ?

You might want something like
#!/bin/bash
for folder in ~/Music/*; do
[ -d "$folder" ] || continue
cd "$folder" || continue
~/Desktop/playlist.sh "$folder" | sort -n > "$folder"/"${folder##*/}".xwaxpls
done
Remember that you do not try sort -o file file, most unix commands don't like to write to an output file that is input at the same time.

Related

Bash script to check if a new file has been created on a directory after run a command

By using bash script, I'm trying to detect whether a file has been created on a directory or not while running commands. Let me illustrate the problem;
#!/bin/bash
# give base directory to watch file changes
WATCH_DIR=./tmp
# get list of files on that directory
FILES_BEFORE= ls $WATCH_DIR
# actually a command is running here but lets assume I've created a new file there.
echo >$WATCH_DIR/filename
# and I'm getting new list of files.
FILES_AFTER= ls $WATCH_DIR
# detect changes and if any changes has been occurred exit the program.
After that I've just tried to compare these FILES_BEFORE and FILES_AFTER however couldn't accomplish that. I've tried;
comm -23 <($FILES_AFTER |sort) <($FILES_BEFORE|sort)
diff $FILES_AFTER $FILES_BEFORE > /dev/null 2>&1
cat $FILES_AFTER $FILES_BEFORE | sort | uniq -u
None of them gave me a result to understand there is a change or not. What I need is detecting the change and exiting the program if any. I am not really good at this bash script, searched a lot on the internet however couldn't find what I need. Any help will be appreciated. Thanks.
Thanks to informative comments, I've just realized that I've missed the basics of bash script but finally made that work. I'll leave my solution here as an answer for those who struggle like me.:
WATCH_DIR=./tmp
FILES_BEFORE=$(ls $WATCH_DIR)
echo >$WATCH_DIR/filename
FILES_AFTER=$(ls $WATCH_DIR)
if diff <(echo "$FILES_AFTER") <(echo "$FILES_BEFORE")
then
echo "No changes"
else
echo "Changes"
fi
It outputs "Changes" on the first run and "No Changes" for the other unless you delete the newly added documents.
I'm trying to interpret your script (which contains some errors) into an understanding of your requirements.
I think the simplest way is simply to rediect the ls command outputto named files then diff those files:
#!/bin/bash
# give base directory to watch file changes
WATCH_DIR=./tmp
# get list of files on that directory
ls $WATCH_DIR > /tmp/watch_dir.before
# actually a command is running here but lets assume I've created a new file there.
echo >$WATCH_DIR/filename
# and I'm getting new list of files.
ls $WATCH_DIR > /tmp/watch_dir.after
# detect changes and if any changes has been occurred exit the program.
diff -c /tmp/watch_dir.after /tmp/watch_dir.before
If the any files are modified by the 'commands', i.e. the files exists in the 'before' list, but might change, the above will not show that as a difference.
In this case you might be better off using a 'marker' file created to mark the instance the monitoring started, then use the find command to list any newer/modified files since the market file. Something like this:
#!/bin/bash
# give base directory to watch file changes
WATCH_DIR=./tmp
# get list of files on that directory
ls $WATCH_DIR > /tmp/watch_dir.before
# actually a command is running here but lets assume I've created a new file there.
echo >$WATCH_DIR/filename
# and I'm getting new list of files.
find $WATCH_DIR -type f -newer /tmp/watch_dir.before -exec ls -l {} \;
What this won't do is show any files that were deleted, so perhaps a hybrid list could be used.
Here is how I got it to work. It's also setup up so that you can have multiple watched directories with the same script with cron.
for example, if you wanted one to run every minute.
* * * * * /usr/local/bin/watchdir.sh /makepdf
and one every hour.
0 * * * * /user/local/bin/watchdir.sh /incoming
#!/bin/bash
WATCHDIR="$1"
NEWFILESNAME=.newfiles$(basename "$WATCHDIR")
if [ ! -f "$WATCHDIR"/.oldfiles ]
then
ls -A "$WATCHDIR" > "$WATCHDIR"/.oldfiles
fi
ls -A "$WATCHDIR" > $NEWFILESNAME
DIRDIFF=$(diff "$WATCHDIR"/.oldfiles $NEWFILESNAME | cut -f 2 -d "")
for file in $DIRDIFF
do
if [ -e "$WATCHDIR"/$file ];then
#do what you want to the file(s) here
echo $file
fi
done
rm $NEWFILESNAME

How do I Batch Rename Folders in OSX?

So I have been trying to rename about 5000 folders based on a CSV (Old name, Newname)
This is a one time operation, once hdkjsh2-2f8c-46b9-bbdb_doc is converted to 3 then it will not need to be touched again.
I have tried the solution here (Setting up an automator workflow with a command script) but found that it does not do a great deal when it comes to folder/directory names and all the guides/documentation is around file names and not folder.
Any suggestions would be much appreciated
Example of CSV
Doc_Location, New_ID
hdkjsh2-2f8c-46b9-bbdb_doc , 3
Please make a backup before trying the following.
Save the following script in your HOME directory as renamer:
#!/bin/bash
cat "file.csv" | while IFS=' ,' read dir new ; do
if [ -d "$dir" ]; then
echo Rename $dir as $new
#mv "$dir" "$new"
else
echo "ERROR: Directory $dir not found - ignored"
fi
done
Then start Terminal and make the script executable by running:
chmod +x $HOME/renamer
Then change directory to where your directories are that need renaming:
cd path/to/things/needing/renaming
Make sure you have your CSV, called file.csv saved in that directory, then run with:
$HOME/renamer
It doesn't actually do anything, it just tells you what it would do. If it looks correct, edit $HOME/renamer and remove the single # on the line that says:
#mv "$dir" "$new"
so that is looks like:
mv "$dir" "$new"
Then be doubly sure you have made a backup and run the script again:
$HOME/renamer
Go to the folder where the other folders you want to rename are located. Select all the folders you want to rename. Then click on the action icon at the top of finder window. This will open a window where one option is to rename x items. See image below.
When you select "Rename x items" you get a box like the one shown below where you can specify the new names.

Need loop to delete parts of file name

I have been using an image optimizer for my websites and when I do this, it gives me files with -compressor at the end of it.
input: filename.jpg
output: filename-compressor.jpg
I need help in creating a batch file or a command script that I can just place these files into a folder and it will loop through all of these and change the names of these for me so that I don't have to go through them one by one.
mkdir -p compressors
mv *-compressor.jpg compressors/
cd compressors
for i in *-compressor.jpg; do j=${i%%\-compressor.jpg}.jpg; mv "$i" "$j"; done

Bash If then that reads a list in a file condition

Here is the condition:
I have a file with all packages installed.
I have a folder with all kinds of other packages, but they include all of the ones in the list, plus more.
I need a bash script that will read the file and check a folder for packages that don't exist in the list then remove them, they are not needed, but keep the packages that are on the list in that folder.
Or perhaps the bash should read folder then if packages in the folder aren't on the list them rm -f that or those packages.
I am familiar with writing if then conditional statements, I just don't know how to do if making the items in the list a variable or variables (in a loop).
thanks!
I would move the packages on the list to a new folder, delete the original folder, and move the temporary folder back:
DIR=directory-name
mkdir "$DIR-tmp"
while read pkgname; do
if [[ -f "$DIR/$pkgname" ]]; then
mv "$DIR/$pkgname" "$DIR-tmp"
fi
done < package-list.txt
# Confirm $DIR-tmp has the files you want first!
rm -rf "$DIR"
mv "$DIR-tmp" "$DIR"
I think you want something like this:
for file in $(ls folder) ; do
grep -E "$file" install-list-file >/dev/null || \
echo $file
done > rm-list
vi rm-list # view file to ensure correct
rm $(<rm_list)
There are ways to make this faster (using parameter substitution to avoid fork/exec's), but I recommend avoiding fancy shell stuff [${file##*/}] until you've got the basics down. Also, this script basically translates the description into a script and is not intended to be much more than a guide on how to approach the problem.

A simple mv command in a BASH script

The aim of my script:
look at all the files in a directory ($Home/Music/TEST) and its sub-directories (they are music files)
find out what music genre each file belongs to
if the genre is Heavy, then move the file to another directory ($Home/Music/Output)
This is what I have:
#!/bin/bash
cd Music/TEST
for files in *
do
if [ -f "$files" ];then
# use mminfo to get the track info
genre=`mminfo "$files"|grep genre|awk -F: '{print $2}'|sed 's/^ *//g'|sed 's/[^a-zA-Z0-9\ \-\_]//g'`
if [ $genre = Heavy ] ;then
mv "$files" "~/Music/Output/$files"
fi
fi
done
Please tell me how to write the mv command. Everything I have tried has failed. I get errors like this:
mv: cannot move ‘3rd Eye Landslide.mp3’ to ‘/Music/Output/3rd Eye Landslide.mp3’: No such file or directory
Please don't think I wrote that mminfo line - that's just copied from good old Google search. It's way beyond me.
Your second argument to mv appears to be "~/Music/Output/$files"
If the ~ is meant to signify your home directory, you should use $HOME instead, like:
mv "$files" "$HOME/Music/Output/$files"
~ does not expand to $HOME when quoted.
By the look of it the problem occurs when you move the file to its destination.Please check that /Music/Output/ exits from your current directory.Alternatively use the absolute path to make it safe. Also it's a good idea not use space in the file-name.Hope this will helps.:)
Put this command before mv command should fix your problem.
mkdir -p ~/Music/Output

Resources