Home > Blockchain >  ignore files with db.zip and copy remaing file in a folder in linux
ignore files with db.zip and copy remaing file in a folder in linux

Time:02-22

I want to ignore files with _db.zip in a folder and copying remaining zip files in a folder in linux.

I have tried as below:

for filename in *;
do 
  extension="${filename#*.}"  ====> giving output as 33_adc_db.zip  
  where here i want output as db.zip
 
  if [ "$extension" != .zip]; then
    echo ""
  fi

Please help me on this as soon as possible.

CodePudding user response:

in one line :

# full path to source dirs
l_src=~/src
# full path to target dirs
l_tgt=~/tgt

find $l_src -type f ! -regex ". _db\.zip" | xargs -I "{}"  mv {} $l_tgt 

each command in details

  1. -type f -- get files only
  2. ! -regex ". _db.zip" -- not like "_db.zip". ". " -- any char "\." -- treat as dot not like any char
  3. xargs -I "{}" -- use symbol "{}" as stdin and get line by line
  4. try this for better understanding find $l_src -type f ! -regex ". _db\.zip" | xargs -I "{}" echo "mv {} $l_tgt" here we just echo commands

CodePudding user response:

I see you are trying to get the extension of the filename, but in order to use a variable, you need to preceed it with a dollar-sign:

Instead of:

extension="${filename#*.}"

Try:

extension="${$filename#*.}"

Is it going better?

Edit: you might also add an extra space before closing the bracket in your if-clause:

if [ "$extension" != .zip ]; then

CodePudding user response:

I created a few files using touch touch a.zip b.zip bash c.x.zip And run this simplified bash script:

#!/bin/bash

for filename in *;
do 
  extension="${filename##*.}"
  echo "${filename}"
  echo "${extension}"
  if [ ${extension} != ".zip" ]; then
  echo "hello"
  fi
done

To get

a.zip
zip
hello
b.zip
zip
hello
c.x.zip
zip
# no hello for c!
  • Related