Call partial file name in a linux script - shell

I have several files: one_001_three.txt, one_002_three.txt, . . .
After removing the extension, I would like to call it such that:
${fname}_001
would call the file 'one_001'
Any ideas?

As far as i understood your Question ..
yu can use this command basename try with this and develop .
basename one_001_three.txt _three.txt -->this will give output as one_001 .
the filename doesnt get changed though .

As far as i understood your Question ..
you can use this command basename try with this and develop .
basename one_001_three.txt _three.txt -->this will give output as one_001 .
the filename doesnt get changed though

Related

What does . ./build.man mean in this piece of code?

build.sh contains such code in the beginning:
. ./build.include
I am new to shell and don't know what this means.
I know that . stands for current directory.
and in the same directory of build.sh there is a file named build.include
But what does it mean if . and ./build.include appears in the same row?
There is a file called build.include in the current directory that will be read in and executed in that point in the script.
In this case, build.include contains a bunch of library functions used by build.sh.

How to include functions from a shell script from a different folder

I have a directory structure of type
|--bringup-scripts
| |--prep.sh
|--scripts
| |--i2c0.func
| |--bit.func
The prep.sh looks like this :
#!/bin/bash
. ../scripts/i2c0.func
The i2c0.func looks like this :
#!/bin/sh
. ./bit.func
As there is a dependency of i2c0.func on bit.func ,If I run prep.sh from the bringup-scripts folder,it throws an error saying
./bit.func: No such file or directory
How should I resolve this?
What you could do is not using relativ paths, instead defining a starting point for all scripts in the prep.sh file like this:
#!/bin/bash
export ROOT_DIR="$(dirname $PWD)"
. $ROOT_DIR/scripts/i2c0.func
You have to include $ROOT_DIR as a prefix for every script in the scripts directory too, e.g.:
i2c0.func
#!/bin/sh
. $ROOT_DIR/scripts/bit.func
I don't know if there's a standard way to do this, but you could try changing i2c0.func like this:
#!/bin/sh
. ../scripts/bit.func
Though note prep.sh gets its caller's working directory, so it will only work when you run it from bringup-scripts/ or scripts/.

Find a file in Cygwin

I use find -name "nametofind" in cygwin to search for a file, but it does not give me any result, even when the file I want to search exists in the current directory. What am I doing wrong? Thanks.
As the comment mentioned more succinctly, you need to tell find which directory you want to search. If you it is the current directory, you should use ..
find . -name "nametofind"
It appears that the OP was trying to either match a partial file name or a file name with a different case. As #devnull mentioned in his comment, the correct solution for either case is to use the following.
find . -iname '*nametofind*'

replace all files with same name

I have a file in multiple folder called PFSound.js
I've updated it so I want to replace all PFSound.js files in those directories for the new one,
is there a way to do it in just one time?
Mac or windows is ok
Thanks!!
On Mac/Linux, you can do this:
find . -type f -name "PFSound.js" -exec cp path/to/new/PFSound.js {} \;
assuming you wish to do that from the current directory downwards and that the new PFSound.js is located somewhere else.
That says.... find, starting at dot (the current directory) all things of type "file" with the name "PFSound.js", and for each one you find, execute the copy command and copy the new PFSound.js into the same place you just found an old one.
you can use a python script for this .
import os
newfilename="the_new_name"
def renamethefile(folderPath):
for fileOrFolder in os.listdir(folderPath):
if os.path.isdir(fileOrFolder) :
renamethefile(fileOrFolder)
continue
else:
os.rename(fileOrFolder, newfilename)
renamethefile("/path/to/the/folder");
i hope this help you .

Query on find command

When i do a find inside a directory i get the output like
./MyWork/a.c
./MyWork/b.c
./mtab
My question is how can use find command in such a way that my output will not show ./
my output should be
MyWork/a.c
MyWork/b.c
mtab
Thanks,
LinuxPenseur
Add -printf "%P\n" at the end.

Resources