简体   繁体   English

删除所有文件和文件夹中的空格的脚本?

[英]Script to remove spaces in all files and folders?

I wrote a script which removes spaces in a single folder/file name.我编写了一个脚本,用于删除单个文件夹/文件名中的空格。 I want to make it work so that it removes all spaces in folder/files name in the directory the script exists.我想让它工作,以便它删除脚本存在的目录中文件夹/文件名中的所有空格。

MY Script:我的脚本:

#!/bin/bash

var=$(ls | grep " ")

test=$(echo $var | sed 's/ //')

mv "$var" $test

How it worked它是如何工作的在此处输入图像描述

在此处输入图像描述

Thank you for helping!感谢您的帮助!

Try this尝试这个

ls | grep " " | while read file_name
do 
    mv "$file_name" "$(echo $file_name | sed -E 's/ +//g')"
done

sed -E is so that you can use some simple regex, and / +/ so it can work in case of multiple consecutive spaces such as sed -E是为了让你可以使用一些简单的正则表达式,并且/ +/所以它可以在多个连续空格的情况下工作,例如 . . And /g so it replaces every occurrences such as foo baa.txt ./g所以它会替换所有出现的地方,例如foo baa.txt

Something like this might work:像这样的东西可能会起作用:

for f in * ; do
    if [[ "$f" =~ \  ]] ; then
        mv "$f" "${f// /_}"
    fi
done

Explanattion:解释:

for f in * ; do

loops over all file names in the directory.循环遍历目录中的所有文件名。 It doesn't have the quirks of ls that make parsing the output of ls a bad idea.它没有ls的怪癖,使解析ls的 output 成为一个坏主意。

if [[ "$f" =~ \  ]] ; then

This is the bash way of pattern matching.这是模式匹配的bash方式。 The \ is the pattern. \是模式。 You need to escape the space with a backslash, otherwise the shell will not recognize it as a pattern.您需要使用反斜杠转义空格,否则 shell 将无法将其识别为模式。

        mv "$f" "${f// /_}"

${f// /_} is the bash way of pattern-substitution. ${f// /_}是模式替换的bash方式。 The // means replace all occurrences. //表示替换所有匹配项。 The syntax is ${variable//pattern/replacement} to replace all patterns in the variable with the replacement.语法是${variable//pattern/replacement}用替换替换变量中的所有模式。

暂无
暂无

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

相关问题 如何编写UNIX命令或脚本来删除当前目录下所有子文件夹中相同类型的文件? - How to write a unix command or script to remove files of the same type in all sub-folders under current directory? 如何仅删除文件的所有行中的前两个前导空格 - how to remove only the first two leading spaces in all lines of a files bash / awk脚本比较两个不同文件夹中所有文件的内容 - bash/awk script to compare the content all the files in two different folders PHP脚本将所有文件和文件夹上载到FTP服务器 - PHP script to upload all files and folders to FTP server Shell脚本删除带空格的文件 - Shell Script to Delete Files with Spaces 使用隐藏文件和名称中带有空格的文件\\文件夹 - Working with hidden files and files\folders that have spaces in the name 删除目录中的所有文件(不要触摸任何文件夹或其中的任何内容) - Remove all files in a directory (do not touch any folders or anything within them) 在Linux脚本中,如何删除当前目录中的所有文件和目录? - In a Linux script, how to remove all files & directories but one, in current directory? 在单个 linux 命令 os 脚本中使用某些条件重命名多个文件夹中的所有文件。 - Rename all files in multiple folders with some condition in single linux command os script. shell脚本删除除不同文件夹中的最后更新文件以外的所有文件 - shell script to delete all files except the last updated file in different folders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM