1

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
  • 1
    The exact command depends on which directory you are currently in?
    – FedKad
    Nov 14, 2020 at 9:35
  • 3
    If you move the file from B/A/C.txt to B/C.txt, you're moving it to it's parent directory, not it's parent's parent directory. Nov 14, 2020 at 18:36
  • Welcome to askubuntu.com! In case you find an answer to help solve a problem don't forget to click either the grey ☑ or the up-pointing arrow/triangle to the left of the text.
    – Hannu
    Nov 21, 2020 at 21:30

4 Answers 4

7

One way would be

mv B/A/C.txt B/

Or

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

For educational purposes

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

Alternatively study the Bash guides at www.tldp.org

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##*/} .. )
1

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
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .