Score:1

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

de flag

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

David avatar
cn flag
What are you trying to do that mv is not good enough?
ByteManager avatar
de flag
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.
David avatar
cn flag
mv moves files not create them use the man command followed by what you want to learn about in a terminal window.
ByteManager avatar
de flag
@David Thanks for suggesting ```man``` command.
waltinator avatar
it flag
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 avatar
it flag
Read `man man` - learn about `man -k`
PonJar avatar
in flag
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
Score:0
sa flag

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

Score:0
in flag

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

mook765 avatar
cn flag
Is it possible to use a function in it's definition (line 19, looks like you want to use `mv` instead of `lmv` here)?
pLumo avatar
in flag
No, I want to use `lmv`, and yes it is totally possible and necessary here to make it recursive.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.