rename multiple files [closed] - linux

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 10 years ago.
Improve this question
I have multiple file like :
abc_e1000g1.pcap.1
abc_e1000g1.pcap.2
I have to rename this to
1abc_e1000g1.pcap
2abc_e1000g1.pcap
Any unix / dos command for this.

I'm sure there are shorter ways do to this, but here's what comes naturally, using unix shell, assuming there's nothing but the files of interest in your present working directory:
for i in *; do mv "$i" "$(echo $i | sed 's/\(.*\)\.\(.*\)/\2\1/')"; done

Maybe this PowerShell Command would work for you.
dir | rename-item -newName { $_.Name -replace '^(.+)\.([0-9]+)$', '$2$1' }

Related

How do I copy grep output to another text file in different directory? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 days ago.
Improve this question
I am trying to copy specific words from a text file in a directory to another using grep. I have the retrieval of the words I want from the text file, now I just am wondering how I would go about moving it another text file, say in my home directory.
Here is the grep command.
grep -E '^.[*ing]{5}$' words
and here is what I have tried
grep -E '^.[*ing]{5}$' words > words.txt $home
grep -E '^.[*ing]{5}$' words > words.txt /home/
Any help is appreciated!
grep -E '^.[*ing]{5}$' words > /home/words.txt

How to replace string with multiple semicolons and special characters using sed in Linux [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 2 years ago.
Improve this question
I have a string "config"
and want to replace with
"server"
using sed in Linux. I tried the below one. But It did not work.
sed -i "s#$"config"#$"server"#g" setup.xml-->
How can I do that? If not sed other options are fine too.
before "config"
after "server"
One example:
sed 's/"config"/"\s\e\r\v\e\r"/' setup.xml
The replacement string has characters with special meaning in sed such as ; # and &. These will all need to be escaped and so:
sed -n 's/"config"/"\&\#115\;\&\#101\;\&\#114\;\&\#118\;\&\#101\;\&\#114\;"/p' <<< '"config"'

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.

Rename large number of files in bash [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a large list of files that I want to rename.
Much like this
So this is what my files look like
something.pcap1
something.pcap10
something.pcap11
something.pcap12
...
something.pcap111
something.pcap1111
essentially I want to rename all of the files so that the numbers get padded with 0's and they are 5 digit numbers.
something.pcap00001
A simple for loop should do the trick (can be script file):
for file in $(ls -1 something.pcap*); do
[[ ${file} =~ ^something.pcap([[:digit:]]*).* ]]
newfile=$(printf "something.pcap%05d" ${BASH_REMATCH[1]})
mv ${file} ${newfile}
done
Something like this?
rename 's/\d+$/sprintf("%05d",$&)/e' soemthing.pcap*
Note: this works with the rename as found in debian and its derivates.
What about something like this?
#!/bin/bash
for i in $(ls something.pcap*); do
q=$(echo $i|sed -e 's/pcap/pcap00000/;s/pcap0*\([0-9]\{6,\}\)$/pcap\1/')
mv $i $q
done
I hope this will help

Trimming linux log files [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 9 years ago.
Improve this question
It seems like a trivial issue, but I did not find a solution.
I have a number of log files in a php installation on Debian/Linux that tend to grow quite a bit and I would like to trim nightly to the last 500 lines or so.
How do I do it, possibly in shell and applying a command to *log?
For this, I would suggest to use logrotate with a configuration to your liking instead of programming your own script.
There might be a more elegant way to do this programmatically, but it is possible to use tail and a for-loop for this:
for file in *.log; do
tail -500 "$file" > "$file.tmp"
mv -- "$file.tmp" "$file"
done
If you want to save history of older files, you should check out logrotate.
Otherwise, this can be done trivially with the command line:
LOGS="/var/log"
MAX_LINES=500
find "$LOGS" -type f -name '*.log' -print0 | while read -d '' file; do
tmp=$(mktemp)
tail -n $MAX_LINES $file > $tmp
mv $tmp $file
done

Resources