How to rename multiple files in Linux? - linux

I want to rename my files in linux from
sub-NDARINVKW1ALL48_ses-year0120161127_task-MID_run-20161127175734_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-MID_run-20161127175734_bold.nii.gz
sub-NDARINVKW1ALL48_ses-year0120161127_task-MID_run-20161127180331_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-MID_run-20161127180331_bold.nii.gz
sub-NDARINVKW1ALL48_ses-year0120161127_task-nBack_run-20161127173234_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-nBack_run-20161127173234_bold.nii.gz
sub-NDARINVKW1ALL48_ses-year0120161127_task-nBack_run-20161127173809_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-nBack_run-20161127173809_bold.nii.gz
sub-NDARINVKW1ALL48_ses-year0120161127_task-rest_run-20161127164822_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-rest_run-20161127164822_bold.nii.gz
to be
sub-NDARINVKW1ALL48_ses-year0120161127_task-MID_run-1_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-MID_run-1_bold.nii.gz
sub-NDARINVKW1ALL48_ses-year0120161127_task-MID_run-2_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-MID_run-2_bold.nii.gz
sub-NDARINVKW1ALL48_ses-year0120161127_task-nBack_run-1_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-nBack_run-1_bold.nii.gz
sub-NDARINVKW1ALL48_ses-year0120161127_task-nBack_run-2_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-nBack_run-2_bold.nii.gz
sub-NDARINVKW1ALL48_ses-year0120161127_task-rest_run-1_bold.json
sub-NDARINVKW1ALL48_ses-year0120161127_task-rest_run-1_bold.nii.gz
Can anyone help?
I have a list of similar files in different directories too. So, I want it to be a generic code that I can use, if possible.

Related

Exclude directories using python dirsync module

I want to sync two different directories using the dirsync module, but exclude some specific folders.
In the documentation (https://pypi.org/project/dirsync/) it says the exclude need to be a regex pattern but I cant quite make it work.
For example, lets say we have these directories
c:\folder1\folder2
c:\folder1\folder3
d:\folder1\
I want to sync c:\folder1\ with d:\folder1\ and exclude folder3, so basically the folder c:\folder1\folder2 will be copied and created in d:\
from dirsync import sync
src = r'c:\folder1'
dst = r'd:\folder1'
sync(src, dst, 'diff', exclude='^folder3')
this won't work and I can't quite understand why.
The exclude option expects a list of regexp:
exclude=['^folder3']

How to resolve system directories paths independently of system locale?

TLDR
I need to get paths to system directories like "Screenshots":
On an English system. I can just use this one:
C:/Users/User/Pictures/Screenshots
How do I get the path to "Screenshots" directory on a non-English system?
C:/Users/User/Pictures/[NAME]
Description
I have a file manager app, it displays system directories and loads them on click.
The app can run system commands via Powershell and use Node.js (preferred)
Problem
The problem is, it only works if the system has English system language.
Currently, to resolve the "Screenshots" directory path, the app simply joins the User directory with the word "Screenshots"
const pictures = electronRemote.app.getPath('pictures')
const screenshots = PATH.join(pictures, 'Screenshots')
link to the line in code
Expectedly, the C:/Users/User/Screenshots path only exists on English systems.
One way to solve this is to use short names, at least on Windows, I know that system directories have short names like SCREEN~1 and WALLPA~1 for Screenshots and Wallpapers directories, but if I use these names the paths will look like this:
C:/Users/User/SCREEN~1 instead of C:/Users/User/Screenshots throughout the app.
And even if I were to run these paths through a function to convert it to readable name, how would I know which word to replace it with? I need to get the name in the system's language.
Are these translations stored somewhere on the system? Can I just retrieve the translated directory name and use that in the code above?
Question
How do I make it to get / resolve the actual path of system directories like Screenshots and Wallpapers, independently of system locale?
If you know how to do it, could you please suggest the solution for all platforms (Win, Mac, Linux)?
Should I just use the short names like SCREEN~1 and then automatically replace all the occurrences in UI and also filter all paths through a function that replaces this short name with the actual path throughout the whole app? Seems like a lot of work, this approach

Renaming multiple zip files with complex name using single command in linux

I have multiple zip files in a folder with names like below:
"abc.zip-20181002084936558425"
How to rename all of them with one command to get result like below:
"abc-20181002084936558425.zip"
I want the timestamp before the extension for multiple filenames. Now every file has different timestamp so renaming should consider that. Can I rename multiple files like this using single command.
Providing your file are really all with the same name convention and you being in the right directory :
for i in *.zip-*; do newName=${i//.zip};mv $i $newName".zip";done
should do the trick.

Python cx_freeze Create dirs for included files in build

Is it possible to create dirs(folders) on cx_freeze build output, cause i include(include_files) many databases files and i want these to be in specific folder etc. I can take them easily from my folders.....
"include_files": ["databases/nations.txt","databases/newafrica.txt",
"databases/neweeurope.txt","databases/neweurope.txt","databases/newmeast.txt","graph.py",
"databases/newnamerica.txt","databases/plates.txt",
"databases/ACN/rigidA.txt","databases/ACN/rigidB.txt",
"databases/ACN/rigidC.txt","databases/ACN/rigidD.txt","databases/ACN/flexibleA.txt",
"databases/ACN/flexibleB.txt","databases/ACN/flexibleC.txt",
"databases/ACN/flexibleD.txt","alternates.xlsx",
but this will just copy all of them in exe build dir and its a mess.
Thanks in advance.
There are several ways you can go around solving the problem.
Method 1 - Using include_files
Rather than ask for each individual text file you could just put the file name in the setup script and leave out the individual text files. In your case it would be like this:
"include_files": ["databases"]
This would copy the entire databases folder with everything in it into you build folder.
Absolute file paths work as well.
If you are going to use the installer feature (bdist_msi) this is the method to use.
You can copy sub folders only using "include_files": ["databases/ACN"]
Method 2 - Manually
Ok it's rather un-pythonic but the one way to do it is to copy it manually into the build folder.
Method 3 - Using the os module
Much the same as method two it would copy the folder into your build folder but instead of coping it manually it would use Python. You also have the option of using additional Python features as well.
Hope I was helpful.

WinSCP - Do not synchronize subdirectories

I am writing winscp script in VBA to synchronize certain files from remote to local.
The code I am using is
""synchronize -filemask=""""*.xlsx"""" local C:\Users\xx\Desktop /JrnlDetailSFTPDirect""
There are three xlsx files: 14.xlsx, 12.xlsx, 13.xlsx. However, seems like it is running through all the files even though it is not synchronizing them. Besides, one folder under JrnlDetailSFTPDirect is also downloaded from remote, which is not expected.
Is it possible to avoid looping through all the files, just selecting those three files and downloading them?
Thanks
There are separate masks for files and folders.
To exclude all folders, use */ exclude mask:
synchronize -filemask="*.xlsx|*/" local C:\Users\xx\Desktop /JrnlDetailSFTPDirect
See How do I transfer (or synchronize) directory non-recursively?
I cannot tell anything regarding the other problem, as you didn't show us names of the files. Ideally, append a session log file to your question. Use /log switch like:
winscp.com /log=c:\writablepath\winscp.log /command ...

Resources