Move files between directories using shell script - linux

I'm new to linux and shell script in general. I'm using a distribution of Debian on the WSL (Windows Subsystem for Linux). I'm trying to write a very simple bash script that will do the following:
create a file in a directory (child-directory-a)
move to the directory it is in
move the file to another directory (child-directory-b)
move to that directory
move the file to the parent directory
This is what I have so far (trying to keep things extremely simple for now)
touch child-directory-a/test.txt
cd child-directory-a
mv child-directory-a/test.txt home/username/child-directory-b
The first two lines work, but I keep getting a 'no such directory exists' error with the last one. The directory exists and that is the correct path (checked with pwd). I have also tried using different paths (i.e. child-directory-b, username/child-directory-b etc.) but to no avail. I can't understand why it's not working.
I've looked around forums/documentation and it seems that these commands should work as they do in the command line, but I can't seem to do the same in the script.
If anyone could explain what I'm missing/not understanding that would be brilliant.
Thank you.

You could create the script like this:
#!/bin/bash
# Store both child directories on variables that can be loaded
# as environment variables.
CHILD_A=${CHILD_A:=/home/username/child-directory-a}
CHILD_B=${CHILD_B:=/home/username/child-directory-b}
# Create both child folders. If they already exist nothing will
# be done, and no error will be emitted.
mkdir -p $CHILD_A
mkdir -p $CHILD_B
# Create a file inside CHILD_A
touch $CHILD_A/test.txt
# Change directory into CHILD_A
cd $CHILD_A
# Move the file to CHILD_B
mv $CHILD_A/test.txt $CHILD_B/test.txt
# Move to CHILD_B
cd $CHILD_B
# Move the file to the parent folder
mv $CHILD_B/test.txt ../test.txt
Take into account the following:
We make sure that all the folders exists and are created.
Use variables to avoid typos, with the ability to load dynamic values from environment variables.
Use absolute paths to simplify the movement between folders.
Use relative paths to move files relatives to where we are.
Another command that might be of use is pwd. It will tell you the directory you are on.

with your second line, you change the current directory to child-directory-a
so, in your third line there is an error because there is no subdirectory child-directory-a into subdirectory child-directory-a
Your third line should be instead :
mv test.txt ../child-directory-b
The point #4 of your script should be:
cd ../child-directory-b
(before that command the current directory is home/username/child-directory-a and after this command it becomes home/username/child-directory-b)
Then the point #5 and final point of your script should be:
mv test.txt ..
NB: you can display the current directory at any line of your script by using the command pwd (print working directory) in your script, it that helps

#!/bin/sh
# Variables
WORKING_DIR="/home/username/example scripts"
FILE_NAME="test file.txt"
DIR_A="${WORKING_DIR}/child-directory-a"
DIR_B="${WORKING_DIR}/child-directory-b"
# create a file in a directory (child-directory-a)
touch "${DIR_A}/${FILE_NAME}"
# move to the directory it is in
cd "${DIR_A}"
# move the file to another directory (child-directory-b)
mv "${FILE_NAME}" "${DIR_B}/"
# move to that directory
cd "${DIR_B}"
# move the file to the parent directory
mv "${FILE_NAME}" ../

Related

How to execute from any directory Bash script that sources other Bash scripts (not using path variable)

I am not trying to execute a Bash script from any directory by adding the script to my Path variable.
I want to be able to execute the script from any directory using the directory path to that file ... but the file I want to execute sources other files, that is the problem.
If I am in directory file with two scripts myFunctions.sh and sourceFunctions.sh
sourceFunctions.sh
#!/bin/bash
source ./myFunctions.sh
echoFoo
myFunctions.sh
function echoFoo()
{
echo "foo"
}
I can run myFunctions.sh and foo will print to console, but If I go up a directory and run myFunctions.sh I get error
cd ..
file/sourceFunctions.sh
-bash: doFoo.sh: command not found
Unless I changed source file/myFunctions.sh to source file/myFunctions.sh in sourceFunctions.sh.
So how can I source independent of my working directory so I can run sourceFunctions.sh from any working directory I want?
Thanks
You have the right idea. Doesn't need to be that complicated though:
source `dirname $0`/myFunctions.sh
I often compute "HERE" at the top of my script:
HERE=`dirname $0`
and then use it as needed in my script:
source $HERE/myFunctions.sh
One thing to be careful about is that $HERE will often be a relative path. In fact, it will be whatever path you actually used to run the script, or "." if you provided no path. So if you "cd" within your script, $HERE will no longer be valid. If this is a problem, there's a way (can't think of it off hand) to make sure $HERE is always an absolute path.
I ended up just using a variable of the directory path to the script itself for the source directory
so
#!/bin/bash
source ./myFunctions.sh
echoFoo
becomes
#!/bin/bash
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
source ${SCRIPTPATH}/myFunctions.sh
echoFoo
source

How can I execute a command anywhere if certain required files are different directories?

Let say the command be my_command
And this command has to be prepared specific files (file1, file2, and file3) in the current working directory.
Because I often use my_command in many different directories, I'd like to keep the certain files in a certain directory and execute my_command without those three files in the working directory.
I mean I don't want to copy those three files to every working directory.
For example:
Directory containing the three files /home/chest
Working directory: /home/wd
If I execute command my_command, it automatically recognizes the three files in /home/chest/
I've thought the way is similar to add $PATH and not the executable files but just files.
It seems like the files needs to be in the current working directory for the vasp_std command to work as expected, I am thinking that you could simply add all files in a include folder in you home directory and then create a symbolic link to this folder from your script. In the end of your script the symbolic link will then be deleted:
#!/bin/bash
# create a symbolic link to our resource folder
ln -s ~/include src
# execute other commands here
# finally remove the symbolic link from the current directory
unlink src
If the vasp_std command require that the files are placed directly under the current working directory you could instead create a symbolic link for each file:
#!/bin/bash
# create link for to all resource files
for file in ~/include/*
do
ln -s $file `basename $file`
done
# execute other commands here
# remove any previously created links
for file in ~/include/*
do
unlink `basename $file`
done

trying to create a file structure with relative path

I'm trying to create a file structure in unix using the relative path, however the directories are being created under my current directory? What am i doing wrong.
mkdir ../folder1 -p folder2/folder3 folder2/folder4 folder5
.. goes back to my home and /folder should go into folder1, but all of this is being done my current directory?
All the filename arguments to mkdir are simply directories to create, they're not interpreted relative to each other. Putting ../folder1 first doesn't make it use that as the starting directory for all the rest.
You can use brace expansion to concatenate all the subdirectory paths to a single directory prefix.
mkdir -p ../folder1/{folder2/{folder3,folder4},folder5}
Also, remember that options usually go before filename arguments.

shell script mv is throwing unhelpful error "No such file or directory" even though i see it

I need to use a shell script to move all files in a directory into another directory. I manually did this without a problem and now scripting it is giving me an error on the mv command.
Inside the directory I want to move files out of are 2 directories, php and php.tmp. The error I get is cd: /path/to/working/directory/php: No such file or directory. I'm confused because it is there to begin with and listed when I ls the working directory.
The error I get is here:
ls $PWD #ensure the files are there
mv $PWD/* /company/home/directory
ls /company/home/directory #ensure the files are moved
When I use ls $PWD I see the directories I want to move but the error afterward says it doesn't exist. Then when I ssh to the machine this is running on I see the files were moved correctly.
If it matters the directory I am moving files from is owned by a different user but the shell is executing as root.
I don't understand why I would get this error so, any help would be great.
Add a / after the path to specify you want to move the file, not rename the directory.
You should try this:
mv $PWD/\* /home/user/directory/
Are your variables properly quoted? You could try :
ls "$PWD" #ensure the files are there
mv "$PWD"/* "/company/home/directory"
ls "/company/home/directory" #ensure the files are moved
If any of your file or directory names contains characters such as spaces or tabs, your "mv" command may not be seeing the argument list you think it is seeing.

Bash, copy files from folder to another

I have a problem with an Uni OS Course assignment.
Basically the task says:
Deliver now a file for assessment. The content of the file is: one line, containing a command that
copies all files with prefix "2016", from directory "ExercisesOS" to directory "OSLab".
Consider the current directory to be "~" when writing such command.
I have already tried with that code:
cp /ExercisesOS/2016* /OSLab
but it performs me two error.
How can I write the correct command?
You probably want to copy from the directory you are working.
To check where you are working:
$ pwd
/home/userdir
To copy from your working directory:
$ cp ExerciseOS/2016* OSLab/
mkdir OSLab && cp /ExercisesOS/2016* OSLab
This solution would assume that the directory 'OSLab' isn't already created.

Resources