I have a bunch of files named
index.html.1
index.html.2
...
I tried to write a bash script to fix all of their extensions
for file in *.html.*
do
mv "$file" "${file%.html.*}.jpg"
done
which made all of the files disappear.
What have I done?!
You have moved each index.html file, one at a time, to the same target filename. Unfortunately all but the last has now been overwritten.
touch index.html.{1,2,3,4,5}
for file in *.html.*; do echo mv "$file" "${file%.html.*}.jpg"; done
Output
mv index.html.1 index.jpg
mv index.html.2 index.jpg
mv index.html.3 index.jpg
mv index.html.4 index.jpg
mv index.html.5 index.jpg
Related
I am trying to rename a file which is the only file in a directory.
It is a podcast download which changes name each day so I don't know what it is called but it always ends in .MP3
I want to rename it to news.mp3
I have tried the following based on another solution on this site but it appends the news to the file
#!/bin/sh
for file in *.MP3; do
mv "$file" "${file/.MP3/news.mp3}"
done
If it's the only file in the directory you can just write the following command:
mv directory_name/* directory_name/news.mp3
In case there are few files or if dir is empty:
shopt -s nullglob
src="/path/to/dir/with/files"
dst="/destanation/folder"
i=1
cd "$src"
for f in *; do
mv "$f" "$dst/new_name_$((i++))"
done
I have a set of .txt files I want to change the extension of, but keeping the original.
I'm trying to cp each file to a "temp", then mv that "temp" to a new file with a new extension.
for file in $#; do
cp ${file} temp; mv temp ${file}.fas
done
I can't find out why it's not working, but nothing happens. I'm new to bash, so any help appreciated.
Read about substring replacement in bash variables.
This will copy all files like foo.txt to foo.fas:
for file in *.txt; do
cp "$file" "${file/%.txt/.fas}"
done
I have a folder called, evaluation, located in, /home/evaluation, which contains this :
Geography
Math
History
English
How do I rename every file inside of this folder, into something like this :
BackupGeography
BackupMath
BackupHistory
BackupEnglish
Here is what I have tried so far :
for file in /home/evaluation/*
do
mv "$file" "${file//Backup}"
done
But it doesn't work unfortunately.
Try this:
for file in /home/evaluation/*
do
mv -i "$file" "$(dirname "$file")/Backup$(basename "$file")"
done
Explanation: dirname gets the directory name (e.g. dirname /home/evaluation/Geography prints "/home/evaluation"), and basename gets the file name without the path (e.g. dirname /home/evaluation/Geography prints "Geography"). So
... "$(dirname "$file")/Backup$(basename "$file")"
--> "/home/evaluation" + "/Backup" + "Geography"
--> "/home/evaluation/BackupGeography"
(Note: + is not a shell operator. At least, not like this. I'm just illustrating how it's parsed.)
Oh, and mv -i will ask what to do if there's a name conflict. Without the -i, it'll silently and irreversibly delete one of the conflicting files. Using mv for bulk moves without -i (or -n) always makes me nervous.
You can do
for file in /home/evaluation/*
do
mv "$file" `dirname "$file"`/Backup`basename "$file"`
done
I am new to Unix shell scripting. I have a small task of moving files into another directory based on their names.
I have a directory named Cars containing .csv files. I have to read the .csv file names, and move each file into another directory based on the filename, as follows:
BMW_c.csv must go into the BMW/c/ directory.
Mercedes_x.csv must go into the Mercedes/x/ directory.
I tried using for and if loop but couldn`t do it till sub directory.
I highly appreciate your help.
Something like this might work
for f in *.csv; do
DIR="$( echo ${f%.*} | tr '_' '/')"
mkdir -p "./$DIR"
mv "$f" "$DIR"
done
It will read all *.csv files, replace '_' by '/' in the file names (with .csv removed), create the directory and move the file. There will be errors if there are no csv files in the current directory.
I have done this and tried,it works.If any modifications or enhancements in the script you can post your answers..
cd /DIR/Cars/BMW
for f in *.csv
do
filename="${f##*/}"
echo ${filename}
if [[ "${f}" == *c* ]]
then
echo "moving files..."
mv "${f}" /BMW/c/
fi
done
cd /DIR/Cars/Mercedes
for f in *.csv
do
filename="${f##*/}"
echo ${filename}
if [[ "${f}" == *x* ]]
then
echo "moving files..."
mv "${f}" /Mercedes/x/
fi
done
I need make multiple folder having the name from file without extension with this command
for i in *.avi; do mkdir "${i%.*}"; done
I don't know how to mv the related file in the folder made...
Thanks for any help
Thanks. Let's say I want to do a general script to record on Textexpander which ask me for the file extension a sort of this :
echo "Type the suffix of the file follewe by enter:"
read ext
for i in *.ext
do
mkdir "${i%.ext}"
mv "$i" "${i%.ext}"
done
But this is not working.
Use the mv command:
for i in *.avi
do
mkdir "${i%.*}"
mv "$i" "${i%.*}"
done