Extract the file name of last slash from path [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 3 years ago.
Improve this question
I am finding the files in specific location now I need to extract the file name which is after last slash from that path without its extension like *.war by using shell scripting.
I tried below to find out the data in the path:
find /data1/jenkins_devops/builds/develop/5bab159c1c40cfc44930262d30511ac7337805fa -mindepth 1 -type f -name '*.war'
Ex.- This folder "5bab159c1c40cfc44930262d30511ac7337805fa" contains multiple .war file like interview.war, auth.war so I am expecting output is interview war.
Can someone please help?

There are much elegant ways to achieve the objectve. The following use awk to achieve the objectiv:
find /data1/jenkins_devops/builds/develop/5bab159c1c40cfc44930262d30511ac7337805fa -mindepth 1 -type f -name *.war | awk -F "/" '{print $NF}' | awk -F "." '{print $1}'
Awk NF returns the number of fields and you can use that to print the last column. First you seperate the columns with / as field seperator in awk and use it to print last column. Then use . as seperator and print the first column to achieve the desired result. It is done in the above script.

Just use basename:
the_path="/data1/jenkins_devops/builds/develop/ab7f302d157d839b4ac3d7917cfa2d550ba2e73e/auth.war"
basename "$the_path" .war

Related

How to rename folders with name DD.MM.YYYY-X to YYMMDDIX on 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 5 months ago.
Improve this question
I have folders inside a directory with names having the pattern DD.MM.YYYY-X where X refers to an index from 0-9 to distinguish folders with names having the same date.
How could I use regex on bash to replace this pattern with YYMMDDIX where
I is an actual I to signal that what follows is the index of the folder
YY is the last two numbers in YYYY
DD and MM are the same as in the original name
Running this script in the same directory containing folders with names having the pattern DD.MM.YYYY-X will copy those folders in the same directory with naming syntax you requested.
script2.sh file containing the following
#!/bin/bash
for dir in $(find . -maxdepth 1 -name "*-*" -type d -exec basename {} \;) ;do
dd=$(cut -d'.' -f1 <<< "${dir}")
mm=$(cut -d'.' -f2 <<< "${dir}")
yyyy=$(cut -d'.' -f3 <<< "${dir}" | cut -d'-' -f1)
yy="${yyyy: -2}"
x="${dir: -1}"
cp -rvi "${dir}" "${yy}${mm}${dd}I${x}"
done
exit 0
Script Output
'22.12.1983-1' -> '831222I1'
'22.12.1982-1' -> '821222I1'
'22.12.1983-0' -> '831222I0'
'22.12.1982-2' -> '821222I2'
ls output after running script
22.12.1982-1 22.12.1982-2 22.12.1983-0 22.12.1983-1 821222I1 821222I2 831222I0 831222I1 script2.sh
Recommendation (Update #1)
It is recommended to update and use unique variable names. Like with a _ prefix.
Here dd= variable can be changed to _dd=... to avoid conflicting/confusing with the dd command.

Sort files in a directory by their text character length and copy to other directory [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 1 year ago.
Improve this question
I'm trying to find the smallest file by character length inside of a directory and, once it is found, I want to rename it and copy it to another directory.
For example, I have two files in one directory ~/Files and these are cars.txt and rabbits.txt
Text in cars.txt:
I like red cars that are big.
Text in rabbits.txt:
I like rabbits.
So far I know how to get the character length of a single file with the command wc -m 'filename' but I don't know how to do it in all the files and sort them in order. I know rabbits.txt is smaller in character length, but how do I compare both of them?
You could sort the files by size, then select the name of the first one:
file=$(wc -m ~/Files/* 2>/dev/null | sort -n | head -n 1 | awk '{print $2}')
echo $file

How to print output twice in Linux? [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 2 years ago.
Improve this question
Which command is use to print the file name twice on output?
I want to write a pipe that List all the files beginning with the character ā€˜Pā€™ on the screen twice in succession.
Something like:
ls -1 | while read i ; do echo $i $i ; done
ā€¦ should do the trick.
ls | sed -E 's/^(P.*)/\1 \1/'
ls, when used with a pipe, puts 1 file per line.
We use sed with extended RE support -E.
We capture the name of any word beginning with P: ^(P.*)
and replace it with itself, a space, followed by itself \1 is a back-reference to what is captured in the parenthesis ( ... ) .
I suggest to use the find utility:
find . -maxdepth 1 -type f -name 'P*' -print -print

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 delete a line that contains a word in all text files of a folder? [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
So, in linux, I have a folder with lots of big text files.
I want to delete all the lines of these files that contain a specific keyword.
Is there any easy way to do that across all files?
There already many similar answers. I'd like to add that if you want to match this is a line containing a keyword but not this is a line containing someoneelseskeyword, then you had better added brackets around the word:
sed -i '/\<keyword\>/d' *.txt
I cannot test this right now, but it should get you started
find /path/to/folder -type f -exec sed -i '/foo/d' {} ';'
find files in the directory /path/to/folder
find lines in these files containing foo
delete those lines from those files
Sure:
for x in files*
do
grep -v your_pattern "$x" > x && mv x "$x"
done
try this:
find your_path_filename |xargs sed -i '/key_word/d'
sed -i '/keyword/d' *.txt -- run this in your directory.
sed - stream editor , use it here for deleting lines in individual files
-i option : to make the changes permenent in the input files
'/keywprd/' : specifies the pattern or the key to be searched in the files
option d : informs sed that matching lines need to be deleted.
*.txt : simply tells sed to use all the text files in the directory as input for
processing , you can specify a individual or a extension like *.txt the way i did.

Resources