Shell or Perl script to rename folders from a text file - linux

I'm trying t look for a way to rename folders in Linux via a shell or perl script based on the first two columns in a .txt file:
Example of text file
rename.txt
current_dir1 new_dir1
current_dir2 new_dir2
current_dir3 new_dir3
...
current_dir50 new_dir50
Then the shell/Perl script would create another file with all the mv commands to run based on what's in the list:
rename_folders.sh or .pl
#!/bin/ksh
mv /home/pete1/current_dir1 /home/pete1/new_dir1
mv /home/pete1/current_dir2 /home/pete1/new_dir2
mv /home/pete1/current_dir3 /home/pete1/new_dir3
........
mv /home/pete1/current_dir2 /home/pete1/new_dir2

#!/usr/bin/env bash
folder_path="/home/pete1/"
while read -r oldname newname; do
# remove echo if happy with result
# if you remove echo it will rename
echo mv "$folder_path/$oldname" "$folder_path/$newname";
done < rename.txt
If you make done < rename.txt to done < rename.txt >somefile.sh it will write all commands which is result of echo

Related

Copy a file from a directory and paste it to multiple sub directories in linux(ubuntu) terminal?

I have a directory mnt/d/LIVI.
Inside the directory, LIVI, I have sub-directories:
mnt/d/LIVI/ak
mnt/d/LIVI/ag
mnt/d/LIVI/few
mnt/d/LIVI/ww4
mnt/d/LIVI/ks5
I wanted to copy a file named tt.txt from mnt/d/LIVI/ak/tt.txt and paste to all the sub directories of LIVI, using Ubuntu terminal. How do i do it using a shell script file?
I tried the following one, but it didn't work.
I created a text file named mnt/d/LIVI/FOLDERS.txt, This listed all the sub directories names.
And saved a script file in mnt/d/LIVI directory. The following is the script
#!/bin/sh
# -*- coding: utf-8-unix -*-
ROOTDIR=$(dirname $0 | xargs readlink -f)
for SIMDIR in cat FOLDERS.txt | xargs readlink -f ; do
cp mnt/d/LIVI/ak/tt.txt $SIMDIR
done
#cd ..
date
You may try this bash script
#!/bin/bash
cd "${0%/*}" || exit
for dir in */; do
if [[ $dir != 'ak/' ]]; then
cp ak/tt.txt "$dir"
fi
done
The script must reside under the diectory mnt/d/LIVI
Don't read lines with for.
(If you really wanted to, the syntax for that would look like
for dir in $(cat FOLDERS.txt); do
...
but really, don't. The link above explains why in more detail.)
I don't see why you want to run readlink on the directories at all?
cd "$(dirname "$0")"
while read -r dir; do
cp ak/tt.txt "$dir"
done <FOLDERS.txt
Note also Correct Bash and shell script variable capitalization

Script for displaying current date in a text file?

I have been trying to make a script in Linux bash that first lists all the files and folders in the homedirectory and then saves the information in a text file with the current date within the textile. So far I have tried this, but the textile is empty:
#! /bin/bash
ls -l /home/user/*/
ls /home/user/*/ > list.txt
today=`date '+%d:%m:%Y'`;
touch "$today.list.txt"
Here's a modified version:
#!/bin/bash
today=`date '+%d:%m:%Y'`
# we use the $HOME variable and use tee to print and write to the file
ls $HOME | tee "$today.list.txt"
Perhaps you are looking for something like this
#! /bin/bash
#List all files in home directory
ls -l ~/
#store datetime in a variable
vartime=`date '+%d:%m:%Y'`
#store all filenames in a variable called list
list=$(ls -l ~/)
#create a variable which contains the name of the file to write to
filename=$vartime+".list.txt"
#Append time to the date.list.txt file
echo "$vartime" >> $filename
#Append the list to the date.list.txt file
echo "$list" >> $filename
Hope this helped

Error While running for loop for renaming multiple file in shell script

While renaming multiple file in AIX using for loop I am getting error
${fn/$eisinno/$efilename}": 0403-011 The specified substitution is not valid for this command.
Input File:
raj_10576_INE728J01019_arya1.pdf
ram_10576_INE728J01019_arya1.pdf
rhaul_10576_INE728J01019_arya1.pdf
sanjay_10576_INE728J01019_arya1.pdf
dinesh_10576_INE728J01019_arya1.pdf
Desired Output File:
raj_10576_Remote_sag.pdf
ram_10576_Remote_sag.pdf
rhaul_10576_Remote_sag.pdf
sanjay_10576_Remote_sag.pdf
dinesh_10576_Remote_sag.pdf
My script is as follow:
#!/bin/bash
eisinno="INE728J01019_arya1.pdf"
evenno=10576
efilename="remote_sag.pdf"
cd /home/rishabh/$eveno
for file in *_$eveno_*.pdf
do
mv -i "${file}" "${file/$eveno_$eisinno/$eveno_remote_$efilename}"
done
Kindly help me
Use double n in the evennos and use braces to make sure where a variable ends:
#!/bin/bash
eisinno="INE728J01019_arya1.pdf"
evenno=10576
efilename="remote_sag.pdf"
cd /home/rishabh/${evenno}
for file in *_${evenno}_*.pdf; do
echo "Debug: ${file} ==> ${file/${evenno}_${eisinno}/${evenno}_remote_${efilename}}"
# Alternative:
echo ${file} | sed "s/${evenno}_${eisinno}/${evenno}_remote_${efilename}/"
mv -i "${file}" "${file/${evenno}_${eisinno}/${evenno}_remote_${efilename}}"
done

Having trouble implementing cp -u in shell script

For a school project, I have a shell script that is supposed to copy the files in two directories (without looking at subdirectories) into a third directory. I'm testing out the -u command so that if two files have the same name, only the newer one will get copied over (that's also a spec). My shell script looks like this (excluding #! and error checking):
cd $1 #first directory
for file in `ls`; do
if [ -f $file ]; then
cp "$file" ../$3 # $3 is the third directory
fi
done
cd ../$2
for file in `ls`; do
if [ -f $file ]; then
cp -u "$file" ../$3
fi
done
My current shell script will copy files that don't exist in directory 3 already, and it won't overwrite a newer file with an older file with the same name. However, my shell script doesn't overwrite an older file with a newer file of the same name in directory 3. I don't think there's anything wrong with the -u command. Can you help find the bug in my code? Thanks!
You are missing the -u option in the first loop:
cp "$file" ../$3 # $3 is the third directory
should instead read:
cp-u"$file" ../$3 # $3 is the third directory

linux bash script to create folder and move files

Hello I need to create folder based on a filename and in this folder create another one and then move file to this second folder
example:
my_file.jpg
create folder my_file
create folder picture
move my_file.jpg to picture
I have this script but it only works on windows and now I'm using Linux
for %%A in (*.jpg) do mkdir "%%~nA/picture" & move "%%A" "%%~nA/picture"
pause
Sorry if I'm not precise but English is not my native language.
#!/usr/bin/env bash
# Enable bash built-in extglob to ease file matching.
shopt -s extglob
# To deal with the case where nothing matches. (courtesy of mklement0)
shopt -s nullglob
# A pattern to match files with specific file extensions.
# Example for matching additional file types.
#match="*+(jpg|.png|.gif)"
match="*+(.jpg)"
# By default use the current working directory.
src="${1:-.}"
dest="${2:-/root/Desktop/My_pictures/}"
# Pass an argument to this script to name the subdirectory
# something other than picture.
subdirectory="${3:-picture}"
# For each file matched
for file in "${src}"/$match
do
# make a directory with the same name without file extension
# and a subdirectory.
targetdir="${dest}/$(basename "${file%.*}")/${subdirectory}"
# Remove echo command after the script outputs fit your use case.
echo mkdir -p "${targetdir}"
# Move the file to the subdirectory.
echo mv "$file" "${targetdir}"
done
Use basename to create the directory name, mkdir to create the folder, and mv the file:
for file in *.jpg; do
folder=$(basename "$file" ".jpg")"/picture"
mkdir -p "$folder" && mv "$file" "$folder"
done
Try the following:
for f in *.jpg; do
mkdir -p "${f%.jpg}/picture"
mv "$f" "${f%.jpg}/picture"
done
${f%.jpg} extracts the part of the filename before the .jpg to create the directory. Then the file is moved there.

Resources