Remove Middle Part of File Name in Linux - linux

I've tried lots and lots of variations using the rename command in the Linux command line and nothing happens when I execute these commands - no errors and no expected outcomes. I've tried using the find command to find files and then rename them with no success. I have files that look like this
201901.cdas1.20190101.pgrbh.grb2flxf06.grb2
201902.cdas1.20190102.pgrbh.grb2flxf06.grb2
and I need them to look like this for the script that is expecting a specific file name format 201901.flxf06.grb2 and 201902.flxf06.grb2.
I need to delete the middle part of the file name with a wild card since there are dates that change in multiple files. The deleted part is this: cdas1.pgrbh.grb2
this is not homework and I've been searching the internet most of the day trying to use different options other than the rename option or a for loop since I get a missing } braces error. Thank you!

Assuming you're using the perl rename command:
rename 's/cdas1\.pgrbh\.grb2//' *.cdas1.20190101.pgrbh.grb2*.grb2

Related

How to move folders with command line prompt in Windows 10?

Let's say we have next directory structure: parent\exampledir\exampledir\<very complicated folder structure like node modules>. By running only one command I want to have next structure: parent\exampledir\<very complicated folder structure like node modules>. How can I achieve this in command prompt?
I tried with: move exampledir\exampledir ., but I get a prompt asking me do I want to override exampledir. After I answer with Yes I get a message saying that access is denied. If I change the name of outer exampledir everything is fine and inner exampledir with all files and folders is correctly moved, but then there is one extra step where I need to delete outer exampledir.
You can do this in one command line, though technically it would be multiple commands using a FOR loop and then first renaming the source directory. You can enter multiple commands on a single line using a semi-colon. The following is written from my head so may not be technically accurate but should guide you to the correct way to write it:
d=blah\exampledir;REN "%d" "%d.bak";MOVE "%d.bak\%~nd" "%d"
This effectively moves the directory to a .bak, and then pulls the subdirectory of the same name back to the original name. If you put this into a batch file, don't forget to escape the % signs with a second one.
Another option could be to add * after the directory name so you MOVE exampledir\exampledir* exampledir\
it may be possible that using Git for Bash, you could launch the shell and use the Linux mv command which may work
Finally, if you want to make sure you back up your batch files, create a free GitHub account and either store it in a repo or create a Gist for one off things

Find linux files with similar names but having different date

I have some files named like this:
aaa-bbb-xxx.ext
aaa-bbb-yyy.ext2
aaa-bbb-zzz.ext3
Some of them are of the same date - then I'm not interested in them. I'd like to find only those files which dates (basically, day of editing) differs. How do I find them? Thank you.
Edit: I forgot to mention - all of files are in the same directory, so there is no need to look up for them on the HDD; the only problem is to make a recognition in the following manner:
1. get 3 files matching the pattern
2. check their dates (day basically) of editing
3. if dates are different, list these files; if the same, ignore
4. continue until all files are checked in the directory
Linux command "Locate"
may help you.

Locating files starting with a string for my hw

I have a conundrum and I am not necessarily trying to cheat or anything I am just simply stuck. I am trying to complete an assignment for my intro to Linux class and I was hoping someone would be able to help me find the right solution. I have to:
In the same directory (where the last file was found) list all files
starting with "host" --
Use the long listing format Use a command to
find the file that shows the name of your computer
now the directory in question is /etc and I have tried several commands to no avail for both of these but especially the first one. I have tried find and locate and even attempted a grep and it just is not working as intended. I can't get files that start with "host" at most I keep getting a list of permission denied or files that end in .host and so I am not sure what I am doing wrong but I really need help so I can turn in my assignment. You don't have to tell me what the exact command should be I am just looking for some guidance again I am not trying to cheat just need help to figure it out.
and welcome to Stack Overflow! Here are some pointers.
See globbing in Linux and the * symbol.
"long listing" is an option for ls command, see ls --help. The name of your computer (or, more accurately, the name of your host) is a file in /etc/. You should see it when doing #1.

Linux terminal script to create boilerplate files in current working directory with one varying word?

I have to create two boilerplate files, both of which always have the same content, with the EXCEPTION of a single word. I'm thinking of creating a command or something that I can run in the Linux terminal (Ubuntu), along with an argument that represents the one word which can vary in the files created. Perhaps a batch file will accomplish this, but I don't know what it will look like.
I will be able to run this command every time I create these boilerplate files, instead of pasting the boilerplate and changing the one word in the file that has to be changed.
These file paths relative to my current working directory are:
registration.php
etc/module.xml
A simple Python script that reads in the file as string and replaces the occurrence would probably be the quickest. Something like:
with open('somefile.txt', 'r+') as inputFile:
txt=inputFile.read().replace('someword', 'replacementword')
inputFile.seek(0)
inputFile.write(txt)
inputfile.close()

AppleScript Replace File

I'd like to write an AppleScript for replacing three system files with ones I've modified. I'd like to do this with an AppleScript instead of manually replacing them because I'll have to replace three files every time there's an OS X update. Specifically, I'll be replacing stock graphics drivers with ones I've modified to support a graphics card which is connected via Thunderbolt. Is it possible to write an AppleScript for replacing one file with another? I ask because I know that when you replace a file, a dialog pops up with three options, and I don't know how to address that.
You can do this with Finder:
set freshFile to choose file
tell application "Finder"
move freshFile to desktop replacing yes
end tell
All you need to do is work out the source and destination paths to completely automate the script.
Many scripters do not like working with Finder, for a variety of reasons. If you want something that is incredibly fast, you would use the do shell script inside of your AppleScript:
do shell script " mv -f ~/Desktop/ArlandaTilUppsala.pdf ~/Documents/Employ.pdf"

Resources