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.
3
Answers
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 :
The point #4 of your script should be:
(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:
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
You could create the script like this:
Take into account the following:
Another command that might be of use is
pwd
. It will tell you the directory you are on.