How do I move a file to its parent's parent directory?

Consider something like B/A/C.txt.

How can I move the C.txt file to its parent's parent directory so the result would be B/C.txt?

3

4 Answers

One way would be

mv B/A/C.txt B/

Or

cd B/A/
mv C.txt ..
0

For educational purposes

$ man bash
Type / and fill in ^ +Parameter Expansion then press ENTER

Alternatively study the Bash guides at

An example to study:

$ f="B/A/C.txt"
$ mkdir -p "${f%/*}"
$ touch "$f"
$ find "${f%%/*}"
...
$ mv "${f}" "${f%/*}/.."
$ find "${f%%/*}"
B
B/C.txt
B/A

Please note: This is NOT a general answer, there are caveats; but might be consider to be very close to it.

More to study, restarted from scratch:

$ f="B/A/C.txt"
$ mkdir -p "${f%/*}"
$ touch "$f"
$ ( cd ${f%/*} && mv ${f##*/} .. )

on Linux terminal mv C.txt ../.. make the trick:

$ mkdir -p /tmp/A/B # create as a temporary dir
$ cd /tmp/A/B # get into dir
$ pwd # show were you are
/tmp/A/B
$ echo 'foo' > C.txt # create a file containing text foo
$ mv C.txt ../.. # move file into parent dir of parent dir
$ cd ../../ # get into there
$ pwd # are we there?
/tmp
$ cat C.txt # check your file.
foo

You can accomplish that by doing

mv B/A/C.txt B/

or

mv B/A/C.txt B/C.txt

or

cd B/A/
mv C.txt ..

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like