Bash script to read .csv file to create/move content - bash

I have a folder containing thousands of images (abc.jpg, 6_08.jpg, refugee_awareness_workshop_by_british_red_cross.jpg etc) and a csv file that contains columnA an ID number (1036, 10028 etc) and column B the image filenames.
Using Automator for Mac I'm going to open a finder file (the .csv) then run a bash script that would create a folder for each ID and move the corresponding image to that folder. A folder can have multiple images.
My bash script so far is:
cd "${1%/*}"
while read line
do
FolderName=${line%,*}
ImageName=${line#*,}
mkdir "$FolderName"
mv "$ImageName" "$FolderName"
done < "$1"
This seems to create the folders fine but then it returns this error:
mv: rename abc.jpg\r to 1030/abc.jpg\r: No such file or directory
And a screenshot of my CSV is as follows:
Any thoughts?

You can remove extraneous carriage returns from ImageName with:
ImageName=$(tr -d '\r' <<< "$ImageName")
You may also like to replace:
mkdir "$FolderName"
with:
mkdir -p "$FolderName"
because it won't emit errors when called with a directory name that already exists - like you say you have.

Related

Batch create folders from TXT file... with subfolders in them

I have a TXT file with 1,000 lines of product numbers I need to make into 1,000 folders with two subfolders in each named IMAGE & SPEC. I want to run it in Automator or from Terminal on my mac but cannot find the answer ANYWHERE! Any help out there?
In Terminal.app, type “cd “ (without the quotes but make sure there is a space after cd). Then drag the folder from Finder (the folder where you want the 1000 new folders created in) Into Terminal.app. This will change the working directory to the directory to where the new folders will be created.
If your products.txt file is located in the same directory as the 1000 new folders will be created, enter this following line of code into the Terminal window and be sure to change the name “products.txt” to your actual file name.
while read line; do mkdir -p "$line/IMAGE"; mkdir -p "$line/SPEC"; done < products.txt
If your products.txt file is located elsewhere, then use this line of code instead.
while read line; do mkdir -p "$line/IMAGE"; mkdir -p "$line/SPEC"; done <
then drag the “products.txt” file from Finder Into Terminal.app. Then it should look something like this (assuming the “products.txt” file was on you Desktop)
while read line; do mkdir -p "$line/IMAGE"; mkdir -p "$line/SPEC"; done < /Users/YourShortName/Desktop/products.txt
you can use pure shellscript for this task like below:
while read line;
do
mkdir -p "$line/IMAGE";
mkdir -p "$line/SPEC";
done < products.txt

How to check if a file is in a dir and then delete it and another file?

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

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.

How to write a bash script using Mac to take files with the same date and put them in a folder with that date

I have hundreds of datalogger files in a directory and I want to write a bash script that will take files with the same date in the filename (an example file name is "2016-06-15T170000_SMARTFLUX.data", where 2016-06-15 is the date) and store them in a folder with the date as the name. I am using a Mac Terminal window, which I believe is Linux (I apologize for my ignorance in computer terminology)
So far I have:
#Type of file (extension) to process:
FILES=*.data
#get date string from file name to use for newly created file
DATE=`ls $FILES|head -n 1|cut -c 1-10`
Any help would be greatly appreciated. I have only modified a bash script that combines these types of files into a text, and I have not created any folders or moved files.
Assuming your script is in the same dir as the data files:
#!/bin/bash
for filename in *.data; do
target_dir=${filename:0:10}
if [[ ! -d $target_dir ]]; then
mkdir $target_dir
fi
mv $filename $target_dir
done

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

Resources