简体   繁体   English

忽略带有 db.zip 的文件并将剩余文件复制到 linux 中的文件夹中

[英]ignore files with db.zip and copy remaing file in a folder in linux

I want to ignore files with _db.zip in a folder and copying remaining zip files in a folder in linux.我想忽略文件夹中带有 _db.zip 的文件,并将剩余的 zip 文件复制到 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.请尽快帮我解决这个问题。

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 -type f -- 只获取文件
  2. . . -regex ".+_db.zip" -- not like "_db.zip". -正则表达式“.+_db.zip”——不像“_db.zip”。 ".+" -- any char "\." “.+”——任何字符“\.” -- treat as dot not like any char -- 像点一样对待任何字符
  3. xargs -I "{}" -- use symbol "{}" as stdin and get line by line xargs -I "{}" -- 使用符号 "{}" 作为标准输入并逐行获取
  4. try this for better understanding find $l_src -type f. -regex ".+_db\.zip" | xargs -I "{}" echo "mv {} $l_tgt"尝试这个以更好地理解find $l_src -type f. -regex ".+_db\.zip" | xargs -I "{}" echo "mv {} $l_tgt" find $l_src -type f. -regex ".+_db\.zip" | xargs -I "{}" echo "mv {} $l_tgt" find $l_src -type f. -regex ".+_db\.zip" | xargs -I "{}" echo "mv {} $l_tgt" here we just echo commands find $l_src -type f. -regex ".+_db\.zip" | xargs -I "{}" echo "mv {} $l_tgt"这里我们只是回显命令

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子句中的括号之前添加一个额外的空格:

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

I created a few files using touch touch a.zip b.zip bash c.x.zip And run this simplified bash script:我使用 touch touch a.zip b.zip bash c.x.zip创建了一些文件并运行这个简化的 bash 脚本:

#!/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!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM