Shell copy a file but changing extension - shell

I am pretty new to linux and shell and I wold like to copy a file to a location passed by an argument but change its extension, the windows version of that would be
copy .\a\b\c.pre %~dpn2.i
d --> get the drive letter only
p --> get the path only
n --> get the file name only
So my question is : How can I get the full path of a file passed by argument and add a file extension to it?
The shell command would look like cp ./a/b/c.pre [$2 path but with extension .i]
Thanks in advance

for file in *.src; do
cp "${file}" "${tgt_dir}"/"${file/%.src/.tgt}"
done
This will simply rename all the files with extension .src in the source folder to .tgt in the target folder. You need to set the target folder in your script

Related

Command not found in ssh but is in direcory

I am using an ssh account that connects to an external server, i have downloaded through guix some software like samtools and bedtools but when i try to use them in my directory it gives me this error:
-bash: samtools: command not found
In my direcory, however, there is the directry guix.profile and if I go into the bin folder of this, I have everything I downloaded.
What am I doing wrong?
Thank you
enter image description here
To run a file from the shell you need two things:
The shell must find the file
Being in the same directory does not enable the shell to find the file. You have to either supply an absolute or relative path the file, or have the directory in your PATH environment variable
In simplest terms this means instead of
$ samtools
try
$ ./samtools
The relative path tells the shell it wants that file
To run it from another directory, either use the whole absolute path, e.g. /home/yourname/samtools , or move the file into a directory that is on your $PATH
The file needs to be executable
If the file is not executable you will need
$ chmod +x ./samtools

Failed to copy list of files to another folder

I have a text file called "list.txt" that contain all the directories of the files that need to be copied to a new folder (dir_newfolder). I wrote the code like below:
for file in $(cat list.txt); do cp ${file} dir_newfolder; done
I got list of errors: cp:"file_name":No such file or directory. The file_names are the lines pulled out from the "list.txt". But when I copy each file_names from the error message and use cp to copy to the new folder. There is no error.
I am using mac os terminal.
Thanks in advance.
Copy a file or folder locally
In the Terminal app on your Mac, use the cp command to make a copy of a file.
For example, to copy a folder named Expenses in your Documents folder to another volume named Data:
% cp -R ~/Documents/Expenses /Volumes/Data/Expenses
The -R flag causes cp to copy the folder and its contents. Note that the folder name does not end with a slash, which would change how cp copies the folder.
in your case:
make sure you are providing correct path list.txt and the correct path for destiny folder, also i mentioned how to access file variable in double quotes , try this code it's working for me
for file in $(cat ~/Documents/list.txt); do cp "$file" ~/dir_newfolder; done

Simple "SendTo" script on Windows

I would like to create a simple script containing :
"C:\Users\Professional\NVEncC_5.24_x64\NVEncC64.exe" --codec h265 --preset quality --profile main10 --tier high -i "Project.mkv" -o "Project-Q27.mkv"
that i can reach by Right Click => SendTo.
I Understand that i need to name the file XXX.bat and paste it in SendTo folder.
But i don't know how to get the filename to dynamically add it to the script instead of "Project.mkv".
Can you help me ?
Thanks !
K.
A .bat file with the following should work.
"C:\Users\Professional\NVEncC_5.24_x64\NVEncC64.exe" --codec h265 --preset quality --profile main10 --tier high -i "%~f1" -o "%~n1-Q27.mkv"
pause
I also added the pause so you'll be able to see what happens if it's quick but it can be removed.
%~f1 is the argument for the file you've right clicked and used "SendTo" on (expanded to the full path).
%~n1-Q27.mkv" is essentially the same as argument %~f1 but without the file extension and it adds -Q27.mkv to whatever the filename was.
So if you right click/SendTo/yourbat.bat on a file called Funny.mkv the command (NVEncC64.exe) will be run on that file and output to a file called Funny-Q27.mkv.
I suggest you take backups of your files before testing so that you do not overwrite any existing files by mistake.
To get the name of the PowerShell .ps1 script being run, you can use the following command from MyInvocation:
$MyInvocation.MyCommand
This will return the .ps1 file object.
To get only the name string you could run:
$MyInvocation.MyCommand.Name
While the answer that #notjustme provided works, I have a small addition to it: the output variable should include the drive and path of the filename that was passed to the batch file to have the output file be created in the same directory as the source file.
"C:\Users\Professional\NVEncC_5.24_x64\NVEncC64.exe" --codec h265 --preset quality --profile main10 --tier high -i "%~f1" -o "%~d1%~p1%~n1-Q27.mkv"
pause
The filename that is passed when you right-click --> SendTo --> your_batch_file.cmd is referenced as %1 or the first parameter. Because it is a filename, it can be parsed and expanded further into its parts.
And as mentioned by #notjustme:
%~f1 expands %1 (the filename passed) to its fully qualified path name
%~n1 expands %1 to its file name only
But there is also:
%~d1 expands %1 to its drive letter
%~p1 expands %1 to its full path
%~x1 expands %1 to its file extension only
If you do not add the %~d1%~p1 to the output file variable, by default, the transcoded file will not be created in the same directory as the source file; rather, it will be created in the directory where your batch file is located.
(Additionally, given rigaya's rather short development windows with NVEncC, you might want to install it to a more generally-named directory that does not include the versioning, e.g., "C:\Users\Professional\NVEncC\" instead of "C:\Users\Professional\NVEncC_5.24_x64\". That way, you do not have to update its path in your batch file with every new release.)

How do I get all the files with a particular extension inside a directory?

I need to get all the files with a particular extension inside a folder without having to cd into that folder in cmd.
Some thing similar to a path in bash: /path/to/my/directory/*.txt
I have tried this path in cmd: D:\path\to\my\directory*.txt
But it gives me No such file or directory.
Actually it works much like in bash (with dir instead of ls, and with backslashs):
dir "D:\path\to\my\directory\*.txt"
Don't forget the backslash in front of "*".

How to rename a string in all file types?

How would I rename a string in all file types not just one file type like .txt in Windows 10? Like for example if all my file names were named Bear-(number) but there are lots of different file types like .html, .php, .pdf, .css, & more how could I rename them all? I don't want to have to list all the file types in the folder... Your help would be appreciated.
In windows 10, use command prompt by clicking on the windows button and typing in cmd
Switch to the directroy you have your files in - for example if they are in a directory called 'temp' enter cd temp.
Another example - if the files are in a nested folder like c:\temp\test - enter cd c:\temp\test
If they are on another drive, you will need to change to that drive by specifying the drive letter and then a colon... eg 'e:'
rename all files rename Bear-00001.* Bear-00002.*
The * at the end will preserve the file extension while changing the file name itself
hth

Resources