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.
Related
EDIT: DIR_trash="trash"
I wrote a function to move a file to current directory.
if [ "$1" == "-u" ]
then
if [ $# == 1 ]
then
echo "Something went wrong. Please make sure you're passing the name of the file/directory after '-u'."
else
if [ -f $DIR_trash/$2.zip ]
then
echo "$2.zip has been found in the trash."
cd
cd $DIR_trash
sed -i "/$2/d" $file7
mv -i /$DIR_trash/$2.zip .
unzip $2.zip
\rm $2.zip
cd
else
echo "$2.zip has not been found in the trash."
fi
fi
fi
As you can see, there is a line of code which says:
mv -i /DIR_trash/$2.zip .
So basically I'm trying to move a file that I passed in argument 2 to current directory, from trash. I always run this script from home directory, which does have trash directory. This is what I get when I run this:
Whenever I manually write this is in the Konsole (from home direcotry) it does work:
rm -u trash/d1 .
I'm out of ideas. Could anyone please help?
Let's say you run the script with the current directory being /some/where, and with the arguments -u and d1. I'll also assume that your home directory is /home/ninini. Let's look at where your script looks for files.
DIR_trash="trash"
if [ -f $DIR_trash/$2.zip ]
You check if /some/where/trash/d1.zip exists.
cd
cd $DIR_trash
Assuming both cd commands succeed, the current directory is now /home/ninini/trash.
mv -i /$DIR_trash/$2.zip .
You're saying to move /trash/d1.zip to the current directory, which is /home/ninini/trash.
Neither the source nor the destination make sense. The source /$DIR_trash doesn't make sense: why would you be looking for a directory called trash under the root directory? And the destination doesn't make sense since you just attempted to change to the trash directory, and now you're attempting to move a file out of the trash directory… into the trash directory.
I can't tell what the correct code is because you didn't say what the script is meant to do. You do say that you want to “to move a file to current directory”; then you must not change the current directory midway through the script! Assuming that the path $DIR_trash/$2.zip from the test command is the correct one, remove the cd commands and write
mv -i -- "$DIR_trash/$2.zip" .
Note that this moves the file from a directory called trash under the current directory. If this isn't what you wanted, you need to change the definition of DIR_trash. It should probably be an absolute path, perhaps
DIR_trash=~/trash
Note also that your script breaks on files containing whitespace and other special characters. Always put double quotes around variable substitutions: "$VAR", not $VAR. (Exception: when you know you need some effect that the double quotes prevent, and you understand why it's safe to leave them out.)
I'm now using Ubuntu, and increasingly using terminal.
I would like to delete files from Trash via command line.
So, I've gotta delete files from ~/.local/share/Trash/files dir.
All right, here's the question:
When I move some file to trash, it also creates a file_name.trashinfo file in ~/.local/share/Trash/info.
How could I automatically delete the corresponding .trashinfo file when I delete something in ../files?
You can use the following script to delete both files simultaneously. Save it in some file in the ~/.local/share/Trash directory, and call then bash <script.sh> <path-to-file-to-be-deleted-in-files-dir>.
A sample call to delete the file test if you named the script del.sh: bash del.sh files/test
#!/bin/bash
file=$1
if [ -e "$file" ] # check if file exists
then
rm -rf "$file" # remove file
base=$(basename "$file")
rm -rf "info/$base.trashinfo" # remove second file in info/<file>.trashinfo
echo 'files deleted!'
fi
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.
I want to move a file to a folder based on its file extension.
example: if the file is .csv,it should move to COMPLETED folder , if the file has any extension other any .csv then it should move to REGULAR folder.
Below is my shell script and its not working. Can you let me know what is the problem with it?
#!/bin/bash
cd /apps/int/apd/$1/work
if ls /apps/int/apd/$1/work/*.csv &> /dev/null; then
mv *.csv /apps/int/apd/$1/COMPLETED
else
/apps/int/apd/$1/Regular
fi
Why do you have to check the existence of *.csv files?
#!/bin/bash
cd /apps/int/apd/$1/work
mv *.csv /apps/int/apd/$1/COMPLETED 2>/dev/null
mv * /apps/int/apd/$1/Regular
Here first .csv files are moved to COMPLETED folder. Then rest of the files are moved to Regular folder.
I am assuming you have created COMPLETED and Regular folders.
Change YOUR_PATH with your specific path and your path for /COMPLETED/ and /REGULAR/.
If I got what you wanted to explain i think your variables look like theese:
/YOUR_PATH/ = /apps/int/apd/$1/work
/COMPLETED/ = /apps/int/apd/$1/COMPLETED
/REGULAR/ = /apps/int/apd/$1/Regular
You can try this. :)
#!/bin/bash
for filename in /YOUR_PATH/*;
do
Path="$filename"
extension="${filename##*.}"
if [ "${extension}" = 'csv' ]; then
mv $Path /COMPLETED/
else
mv $Path /REGULAR/
fi
done
If you need anything pls leave a comment. :)
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.