user15788830
user15788830

Reputation:

Move files between directories using shell script

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:

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.

Upvotes: 0

Views: 11144

Answers (3)

AKSiS
AKSiS

Reputation: 1

#!/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}" ../

Upvotes: 0

guzmonne
guzmonne

Reputation: 2530

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:

  1. We make sure that all the folders exists and are created.
  2. Use variables to avoid typos, with the ability to load dynamic values from environment variables.
  3. Use absolute paths to simplify the movement between folders.
  4. 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.

Upvotes: 1

Jean-Loup Sabatier
Jean-Loup Sabatier

Reputation: 779

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

Upvotes: 0

Related Questions