inserting text in filename using linux shell script [closed] - linux

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a couple of files that needs to be renamed by using a linux shell script. I need to insert the text "ID" next to the second character of the filenames:
HP0001.txt
HP0002.txt
HP0003.txt
the script should be able to rename it to
HPID0001.txt
HPID0002.txt
HPID0003.txt

If it's just a "couple of files", it's probably easiest to just rename them manually.
Otherwise, here's a trivial script you can adapt:
for f in HP*.txt; do
f2=`echo $f|sed -e 's/HP/HPID/'`
echo mv $f $f2
done
SAMPLE OUTPUT:
ls HP*
HP001.txt HP002.txt HP003.txt
bash ./tmp.sh
mv HP001.txt HPID001.txt
mv HP002.txt HPID002.txt
mv HP003.txt HPID003.txt

Files in directory before script execution :
HP0001.txt
HP0002.txt
HP0003.txt
main
main script content
cat main.sh
for file in *
do
new_name=$(echo $file|sed 's/HP/&ID/')
mv $file $new_name 2>/dev/null
done
Directory contents after script execution:
ls -1
HPID0001.txt
HPID0002.txt
HPID0003.txt
main

Related

Parsing a conf file in bash [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Here's my config file
#comment 1
--longoption1
#comment 2
--longoption2
#comment 3
-s
#comment 4
--longoption4
I want to write a bash script that will read this .conf file, skip comments and serialize the commandline options like so.
./binary --longoption1 --longoption2 -s --longoption4
Working off of this post on sed, you just need to pipe the output from sed to xargs:
sed -e 's/#.*$//' -e '/^$/d' inputFile | xargs ./binary
As Wiimm points out, xargs can be finicky with a lot of arguments and it might split it up across multiple calls to binary. It may be better off to use sed directly:
./binary $(sed -e 's/#.*$//' -e '/^$/d' inputFile)

Create mutiple files in multiple directories [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I 've got tree of folders like:
00 -- 0
-- 1
...
-- 9
...
99 -- 0
-- 1
...
-- 9
How is the simplest way to create in every single subfolders a file like:
/00/0/00_0.txt
and save to every files some kind of data?
I tried with touch and with loop but without success.
Any ideas how to make it very simple?
List all directories using globs. Modify the listed paths with sed so that 37/4 becomes 37/4/37_4.txt. Use touch to create empty files for all modified paths.
touch $(printf %s\\n */*/ | sed -E 's|(.*)/(.*)/|&\1_\2.txt|')
This works even if 12/3 was just a placeholder and your actual paths are something like abcdef/123. However it will fail when your paths contain any special symbols like whitespaces, *, or ?.
To handle arbitrary path names use the following command. It even supports linebreaks in path names.
mapfile -td '' a < <(printf %s\\0 */*/ | sed -Ez 's|(.*)/(.*)/|&\1_\2.txt|')
touch "${a[#]}"
You may use find and then run commands using -exec
find . -type d -maxdepth 2 -mindepth 2 -exec bash -c 'f={};
cmd=$(echo "${f}/${f%/*}_${f##*/}.txt"); touch $cmd' \;
the bash substitution ${f%/*}_${f##*/} replaces the last / with _

How to rename files with filename from one txt file to filename from another txt file in bash? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
I have some filename in a .txt file and some other filename in another .txt file. I want to rename files stored in that folder from filename in the text file line by line.
FROM :
$cat oldname.txt
file1.mp4
file2.mp4
TO :
$cat newname.txt
video1.mp4
video2.mp4
I want some bash script that can execute mv command line by line for each file.
Like
$mv file1.mp4 video1.mp4
Use a proper loop over the file to rename with bash . Open the files separately in different file-descriptors.
#!/bin/bash
while read oldname <&3 && read newname <&4
do
mv "$oldname" "$newname"
done 3<oldname.txt 4<newname.txt
Try:
while read oldname; do
read -u 3 newname
echo mv $oldname $newname
done < oldname.txt 3< newname.txt
This will merely echo the commands. If you like the result, omit the echo.

Linux rename time stamped file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i have some files with timestamp (XXX_20160125-17.dmp) and i want to rename to XXX_20160124.dmp (-1 day, and only YYYYMMDD).
I try a few things but doesn't work. Thanks.
ls *.dmp |
perl -lne '
m/(.*_)(\d\d\d\d)(\d\d)(\d\d).*(\.dmp)/;
chomp($d = qx(date -d"$2-$3-$4 - 1 day" +%Y%m%d));
#rename $_, "$1$d$5" or die "rename $_ -> $1$d$5: $!\n";
print "mv $_ $1$d$5";
'
This generates commands like:
mv XXX_20160101-19.dmp XXX_20151231.dmp
mv XXX_20160125-17.dmp XXX_20160124.dmp
Assuming the filenames have no spaces, you can pipe the result into bash.
Or uncomment the 'rename' stmt in the perl script.

Grep script to run on CD [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Im trying to learn some bash script writing and cleaning out some old CDs (remember those?)
What I have is a bunch of backup CDs with no rhyme or reason to them so I'd like to create a grep script that will search them based on keywords. (I'm currently trying to test the script first against the desktop) I know how to run grep, but its the scripting I'm having trouble with.
So here's what I have:
#!/bin/bash
#CDGrepScript
SOURCEDIR=/home/name/Desktop (I'm currently testing it with files on the desktop)
#mount /dev/cdrom
#SOURCEDIR=/dev/cdrom
echo "hello, $USER. I will search your files"
echo Begin Search...
grep -ir "taxes|personal|School" *
echo $results
echo "The search is complete. Goodbye"
exit
Now when I run this against files on the desktop. My script hangs after "Begin search" What am I doing wrong?
Thanks for the help
You might be served better by a more general tool. Like a rgrep (recursive grep) that will walk through a tree searching for a search term. An example:
# rgrep
#
# Search for text strings in a directory hierarchy
set +x
case $# in
0 | 1 )
# Not enough arguments -- give help message
echo "Usage: $0 search_text pathname..." >&2
exit 1
;;
* )
# Use the first argument as a search string
search_text=$1
shift
# Use the remaining argument(s) as path name(s)
find "$#" -type f -print |
while read pathname
do
egrep -i "$search_text" $pathname /dev/null
done
;;
esac
Put this in your path, then you just change directories to the mount point for the CD-ROM, and type
$ rgrep "taxes" .
Or whatever other search you wish to perform.

Resources