0

Is mv the only command that we can use to move files/folders on Ubuntu 20.04?

7
  • 1
    What are you trying to do that mv is not good enough?
    – David
    Mar 1, 2022 at 16:38
  • I'm learning Ubuntu/linux commands. I'm making a list of commands according to their primary and secondary functions. While listing, I found that both the commands touch, and > can create new files (I have heard that there's a difference between how these two works). So I want to know all the set of commands which can perform the same function under some special conditions.
    – user1574528
    Mar 1, 2022 at 16:52
  • 3
    Classifying into "primary and secondary" functions is the wrong learning path. A program does what it does, all of its "functions" are "primary" depending on what the current job requires.
    – waltinator
    Mar 1, 2022 at 17:43
  • 2
    Read man man - learn about man -k
    – waltinator
    Mar 1, 2022 at 17:44
  • 1
    It’s good to try to learn this stuff but maybe you are setting your sights too high. I’ve been in and around Linux for over 10 years and I still learn new stuff every month. The key to this is to know where to look to find the command you need. A simple google search with something like Linux command cheat sheet will provide plenty of lists categorised in various ways. You will find you use some commands more frequently. Those are the ones you need to learn, especially when there are loads of options that modify the effect of the command
    – PonJar
    Mar 1, 2022 at 18:45

2 Answers 2

0

The way the mv tool is programmed to move a file is to leave the file's unchanged (unless the file is being moved to a different filesystem) and preserve its ownership and permissions. Other tools provide different options. The cp command, unlike mv, creates a brand new data object in your filesystem that has a new inode location.

You can mimic a move without mv using the built-in cp and rm commands:

cp example/foo .
ls -l -G -g --inode
7476869 -rwxrwxr-x. 29545 Aug  2 11:58 foo
rm example/foo

Source: Revised from Moving files on Linux without mv

0
0

mv across file systems

  • copy the file and remove original file.

For files:

cp source target && rm source

For directories:

cp -r source target && rm -rf source

mv to the same file system:

  • create a new link to your inode and remove the original link.

So, you can mimic a move on the same file system with ln && rm:

ln source target && rm source

However, this only works for files.
To move a directory recursively, you could make a small script/function:

#!/bin/bash
lmv()(

  set -eu

  s="$1"
  t="$2"

  if [ -f "$s" ]; then
    printf '%s -> %s\n' "$s" "$t"
    ln "$s" "$t"
    rm "$s"
  elif [ -d "$s" ]; then
    printf '%s -> %s\n' "$s" "$t"
    mkdir "$t"
    shopt -s dotglob
    for e in "$s"/*; do
      lmv "$e" "$t/${e##*/}"
    done
    rmdir "$s"
  else
    >&2 printf 'No file or directory: %s\n' "$s"
    exit 1
  fi
)

Usage:

lmv source target

(Note: this script is just a prove of concept and there is stuff missing, e.g. multiple sources or preserve owner and permissions, etc.)

2
  • Is it possible to use a function in it's definition (line 19, looks like you want to use mv instead of lmv here)?
    – mook765
    Mar 3, 2022 at 11:20
  • No, I want to use lmv, and yes it is totally possible and necessary here to make it recursive.
    – pLumo
    Mar 3, 2022 at 11:40

You must log in to answer this question.