Output the Names of Copied Files in Shell Script - bash

I am working on a bash script to copy configuration files to their appropriate directories. I would like to output the name of the file that I have copied to the user. Here is what I have so far:
CONFIG_DIR="/home/stackoverflow/app/configs"
CONFIG_FILE="*.config"
cp $CONFIG_FILE $CONFIG_DIR
echo "$CONFIG_FILE was copied to $CONFIG_DIR"
The output I get from this is:
*.config was copied to /home/stackoverflow/app/configs
Let's assume that stackoverflow.config is the only .config file present in the directory where the script is running. I would like it to say:
stackoverflow.config was copied to /home/stackoverflow/app/configs

Either add -v to copy and have it print all the file names (one name per line) or change the echo to
echo $CONFIG_FILE was copied to $CONFIG_DIR
i.e. remove the double quotes (they are not necessary in this example).

Move the $CONFIG outside the quotes, in order to allow the shell to expand its wildcards:
echo $CONFIG_FILE "was copied to $CONFIG_DIR"

CONFIG_DIR="/home/stackoverflow/app/configs"
for file in *.config
do
cp $file $CONFIG_DIR
echo $file was copied to $CONFIG_DIR
done

Related

How to apply a command to subfolders in a folder, and create a new folder with each output?

I have several subfolders in a folder which each contain a file called "README.txt". I would like the following two commands to be applied to each subfolder:
#identify sequence from README.txt where a line contains 'xyz'
seq= sed -nE '/xyz/{ s/^.*Series:([^,]+),.*/\1/; p; }' README.txt
echo $seq
#apply this command to convert filetype and copy the file of interest
dcm2niix_afni $seq -o /subfolder-directory /my-directory
How do I apply these commands to the subfolders in my folder?
I would like the output of the commands to be placed into a new folder, of the same name as the subfolder from which the file is from.
Thank you!
#sk23 It would be much better to provide:
An example of the README.txt file context.
What output exactly do you expect from the sed command?
What is the output of the dcm2niix_afni command - it is a newly generated file probably?
Clarify in detail what you mean by "I would like the output of the commands to be placed into a new folder, of the same name as the subfolder from which the file is from."
But until then you may review the following script as a hint to be completed to reach your requirements.
#!/bin/bash
## User defined Variables
# Main folder path
_mainFolder='./main_folder'
# File name to process
_fileName='README.txt'
while IFS='' read -r _file ;do
seq="$(sed -nE '/xyz/{ s/^.*Series:([^,]+),.*/\1/; p; }' "${_file}")"
## If you need to redirect the `sed` command output to a file
## let's say `README.txt-sed.log` in each relevant folder, un-comment the next line.
# echo "${seq}" > "${_file}-seq.log"
echo "Sed output for the ${_file}"
echo "${seq}"
# Any other relevant command may be added from here.
# To here.
done <<< "$(find "${_mainFolder}" -name "${_fileName}" -type f)"
exit 0

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

unix shell scripting- Using mv command in if condition

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. :)

Bash, Move file help in variable

for i in *.txt
do
#Text files
echo $i
#checking for existing files
if [ -f ~/txt/$i ]
then
j=1
#Stripping .txt from the files
temp=${i%".txt"}
#appending filaname with counter "($j)"
i=$temp\($j\).txt
#move to folder /txt
mv $i ~/txt
else
mv $i ~/txt
fi
done
My loop checks a folder for an existing file, if that file name exists, the file name is appended (ex (1), (2) etc.
Once the file name has been renamed and it is held in $i I try to mv it but I'm getting:
mv: cannot stat 'list(1).txt': No such file or directory
I tried mv {$i} ~/txt, mv [$i] ~/txt etc...no luck. Any ideas?
You are overwriting the actually name of the file here:
i=$temp\($j\).txt
Instead, use a new variable for the new name. Something like this.
newname=$tmp\($j\).txt
#move to folder /txt
mv $i ~/txt/$newname
You say:
Once the file has been renamed and it is held in $i...
But that is wrong - the file has not been renamed at this point.
You manipulated some text in a variable. That does not have any effect on the filesystem until you run a command, such as through using mv.
Also, in your else statement, it is not clear to me why you are running mv on a file that does not exist (fails the -f test).
I had some code here, but from reading your program again, I'm not sure exactly what you're trying to accomplish, exactly...

Simple Bash Script File Copy

I am having trouble with a simple grading script I am writing. I have a directory called HW5 containing a folder for each student in the class. From my current directory, which contains the HW5 folder, I would like to copy all files starting with the word mondial, to each of the students' folders. My script runs but does not copy any of the files over. Any suggestions?
#!/bin/bash
for file in ./HW5; do
if [ -d $file ]; then
cp ./mondial.* ./$file;
fi
done
Thanks,
The first loop was executing only once, with file equal ./HW5. Add the star to actually select the files or directories inside it.
#!/bin/bash
for file in ./HW5/*; do
if [ -d "$file" ]; then
cp ./mondial.* ./"$file"
fi
done
As suggested by Mark Reed, this can be simplified:
for file in ./HW5/*/; do
cp ./mondial.* ./"$file"
done

Resources