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.

  • 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? – Stephen Harris May 25 '22 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' – GeneralTully May 25 '22 at 22:14
  • I do not need to delete the source directory entries, I do change the destination directory depending on what the content is. Mainly just trying to paste the name of the final directory to be copied as quickly as possible mouselessly if I can, or perhaps even split up the cp -al command so that I can tab though filenames in the current directory, if such a thing is possible. – GeneralTully May 25 '22 at 22:18
  • 1
    @roaima makes sense will do – GeneralTully May 26 '22 at 01:55
  • 3
  • 2
    You could do cp -al {sourceDir,dstDir}/Madagascar.2005.1080p.BluRay.DTS.x264 – muru May 26 '22 at 06:48
  • " I am trying to copy the folder structure + hardlink the file/files inside." Please edit your question and describe what you actually want to do. If you want to mimic the complete folder structure, use e.g. find ... -exec ... twice (once to make the all the directories, once to hardlink all the files). But maybe you want to do something different, I still don't know. – dirkt May 26 '22 at 07:06

3 Answers3

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

  • 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. – GeneralTully May 26 '22 at 02: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 '22 at 05: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. – GeneralTully May 26 '22 at 06: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. – Marcus Müller May 26 '22 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. – GeneralTully May 26 '22 at 18:20
  • it's annoyed you enough that you asked here! seriously, sudo apt install zsh; zsh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" is something worth trying (you can then use zsh). You don't have to switch your shell to zsh forever, but honestly... uff, plain bash is just disappointingly less user-friendly. – Marcus Müller May 26 '22 at 19:27
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"  
}  
Prem
  • 3,342
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/
Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • 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. – GeneralTully May 26 '22 at 06:41
  • @GeneralTully then my first suggestion will do exactly that. Try it to a temporary directory to see its effect. – Chris Davies May 26 '22 at 08:17