0

I'm controlling a Linux based NAS through SSH on a Windows computer. What I'm trying to do is use the cp -al command to rapidly copy folders from one directory to another, hardlinking all the files inside. Currently what I do is I list the directory with ls, then I highlight it with my mouse, copy it with right click, then paste it back in, for example if I'm hardlinking a directory called "Madagascar.2005.1080p.BluRay.DTS.x264" I would copy only the name of that directory, then manually write out cp -al "sourceDir/Movie/ then I paste in the "Madagascar.2005.1080p.BluRay.DTS.x264". I do the same for the second directory to build the whole command:

cp -al "sourceDir/Madagascar.2005.1080p.BluRay.DTS.x264" "dstDir/Movie/Madagascar.2005.1080p.BluRay.DTS.x264"

This whole process is pretty cumbersome, and feels unnatural when everything else done on linux shell is done without the mouse.

Is there a better way of building this long command without having to copy and paste things using the mouse, or to type out entire names manually?

edit: I do not want to delete the source directory. The source directory looks like this aproximately:

'Lost S04 720p BluRay DTS x264
 Lost.S03.720p.BluRay.DTS.x264
'Million.Dollar.Extreme.Presents.World.Peace.S01.1080p'
'The Simpsons S01 1080p DSNP WEBRip DDP5.1 x264'
'The Simpsons S02 1080p DSNP WEBRip DDP5.1 x264'

I will be changing the source or destination directories frequently.

7
  • You may want to show the output of the ls command. Is this the only entry in sourceDir, or is there other content? Is the target always dstDir/Movie ? Do you want to remove the sourceDir entry afterwards? May 25, 2022 at 22:04
  • 'Lost S04 720p BluRay DTS x264' Lost.S03.720p.BluRay.DTS.x264 'Million.Dollar.Extreme.Presents.World.Peace.S01.1080p.AMZN.WEBRip.DDP5.1.x264-AJP69[rartv]' 'The Simpsons S01' 'The Simpsons S02' May 25, 2022 at 22:14
  • 1
    @roaima makes sense will do May 26, 2022 at 1:55
  • 3
    Does this answer your question? Quickest way to rename files, without retyping directory path
    – muru
    May 26, 2022 at 6:48
  • 2
    You could do cp -al {sourceDir,dstDir}/Madagascar.2005.1080p.BluRay.DTS.x264
    – muru
    May 26, 2022 at 6:48

3 Answers 3

1

So, first of all, you clearly are trying to work as if you had a proper file manager. That's not a crime, and you should use one :)

Traditionally, Midnight Commander is the tool you should use, mc; chances are you can directly install it on your NAS, even!

In this day and age, ranger (you say you're on debian, so sudo apt install ranger it is.) is probably closer to what you want, it feels a lot more modern. You can select multiple files in any "pane" with space, and then press @ to run a command on them: You'll see :shell %s at the bottom of the screen. The %s will be expanded to all the selected file names when you hit enter, so just type in your command, so that there's :shell cp -al -t /dstDir/Movie %s and hit enter. That's it.

However, ranger is a mighty tool (and so is mc, which at least has more prominent menus!), and 10 minutes with a tutorial will probably make a world of a difference.


without having to copy and paste things using the mouse, or to type out entire names manually?

You need to have heard about tab-completion that all but the most rudimentary shells have! Type the first letters of any file name, hit tab. If there's only one file that starts like that, it gets completed. If there's multiple choices, press tab twice to get a list of candidates. (If you're using zsh with oh-my-zsh, or some of the many other good shell extenders instead of stock bash, you will get to enjoy even more handy completion).

6
  • Thank you, I hadn't heard of Ranger, I'm going to try that out shortly. For some reason tab completion is completely broken on my system, tab does literally nothing but make an error sound. May 26, 2022 at 2:09
  • 1
    Tab completion is absolutely the way to go. Try fixing it. I wouldn't want to live in a world without it.
    – Stewart
    May 26, 2022 at 5:57
  • Turns out it does work I was just getting baited by case sensitivity, turns out literally all the files I was trying to autocomplete were uppercase first letter. May 26, 2022 at 6:37
  • @GeneralTully for most shells, there's ways to turn it case-insensitive. As said, zsh + oh-my-zsh is way nicer than stock bash. May 26, 2022 at 17:22
  • Thanks for the idea, it's not really that annoying now that I'm aware of it, I'm just used to Windows though so it caught me off guard. May 26, 2022 at 18:20
1

The fastest way to Copy-Paste long file names would be to not Copy-Paste at all !

(1) Use a variable:

Eg:

cd sourceDir/  
OneDir=$(ls -d Mada*) # Make sure you get only One Directory ; no Copy with mouse !  
echo $OneDir # verify  
cp -al "$oneDir" "../dstDir/Movie/$OneDir" # Navigate to Dest Directory ; no Paste with mouse !  
OneDir=$(ls -d T*Sim*S01*)  
echo $OneDir  
cp -al $oneDir ../dstDir/Movie/$OneDir  
OneDir=$(ls -d T*Sim*S02*)  
echo $OneDir  
cp -al $oneDir ../dstDir/Movie/$OneDir  

(2) Augment that with a script to automate it:

# Pseudo-Code  
cd sourceDir/  
for OneDir in $(ls -d *) {  # Use the "for" Syntax (& Quoting Syntax) according to your Shell  
    echo $OneDir  # You could include interactivity, to ask whether to Process Current OneDir or not  
    cp -al "$oneDir" "../dstDir/Movie/$OneDir"  
}  
1

You say that you want to,

use the cp -al command to rapidly hard link folders from one directory to another

But then you show a process for copying a file at a time.

If you actually want to copy a directory and its contents, this command will do it all in one step:

cp -al sourceDir/. dstDir/Movie/

Perhaps you want to move selected files from your sourceDir to different dstDir directories. You can use wildcards to pick up one or more files matching a pattern, or Tab to autocomplete a partially typed name (hit it a second time to see the set of remaining matches):

cp -al sourceDir/Lost* dstDir/Movie/
2
  • Sorry about the confusing language, all my Movies are in directories with only one file in them, so it's easy to mix up the words. I am trying to copy the folder structure + hardlink the file/files inside. May 26, 2022 at 6:41
  • @GeneralTully then my first suggestion will do exactly that. Try it to a temporary directory to see its effect. May 26, 2022 at 8:17

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