bash 命令行查找目录中的第一个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14509806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:15:53  来源:igfitidea点击:

command line find first file in a directory

bashdirectoryfindrename

提问by user2008746

My directory structure is as follows

我的目录结构如下

Directory1\file1.jpg
          \file2.jpg
          \file3.jpg

Directory2\anotherfile1.jpg
          \anotherfile2.jpg
          \anotherfile3.jpg

Directory3\yetanotherfile1.jpg
          \yetanotherfile2.jpg
          \yetanotherfile3.jpg

I'm trying to use the command line in a bash shell on ubuntu to take the first file from each directory and rename it to the directory name and move it up one level so it sits alongside the directory.

我试图在 ubuntu 上的 bash shell 中使用命令行从每个目录中获取第一个文件并将其重命名为目录名称并将其向上移动一个级别,以便它位于目录旁边。

In the above example:

在上面的例子中:

  • file1.jpgwould be renamed to Directory1.jpgand placed alongside the folder Directory1

  • anotherfile1.jpgwould be renamed to Directory2.jpgand placed alongside the folder Directory2

  • yetanotherfile1.jpgwould be renamed to Directory3.jpgand placed alongside the folder Directory3

  • file1.jpg将重命名为Directory1.jpg并放置在文件夹旁边Directory1

  • anotherfile1.jpg将重命名为Directory2.jpg并放置在文件夹 Directory2 旁边

  • yetanotherfile1.jpg将重命名为Directory3.jpg并放置在文件夹旁边Directory3

I've tried using:

我试过使用:

find . -name "*.jpg"

but it does not list the files in sequential order (I need the first file).

但它没有按顺序列出文件(我需要第一个文件)。

This line:

这一行:

find . -name "*.jpg" -type f -exec ls "{}" +;

lists the files in the correct order but how do I pick just the first file in each directory and move it up one level?

以正确的顺序列出文件,但如何仅选择每个目录中的第一个文件并将其向上移动一个级别?

Any help would be appreciated!

任何帮助,将不胜感激!

Edit: When I refer to the first file what I mean is each jpg is numbered from 0 to however many files in that folder - for example: file1, file2...... file34, file35 etc... Another thing to mention is the format of the files is random, so the numbering might start at 0 or 1a or 1b etc...

编辑:当我提到第一个文件时,我的意思是每个 jpg 的编号从 0 到该文件夹​​中的多个文件 - 例如:file1、file2……file34、file35 等……另一件事要提到是文件的格式是随机的,因此编号可能从 0 或 1a 或 1b 等开始...

回答by kojiro

If firstmeans whatever the shell glob finds first (lexical, but probably affected by LC_COLLATE), then this should work:

如果first意味着 shell glob 首先找到的任何内容(词法,但可能受 影响LC_COLLATE),那么这应该有效:

for dir in */; do
    for file in "$dir"*.jpg; do
        echo mv "$file" "${file%/*}.jpg" # If it does what you want, remove the echo
        break 1
    done
done

Proof of concept:

概念证明:

$ mkdir dir{1,2,3} && touch dir{1,2,3}/file{1,2,3}.jpg
$ for dir in */; do for file in "$dir"*.jpg; do echo mv "$file" "${file%/*}.jpg"; break 1; done; done
mv dir1/file1.jpg dir1.jpg
mv dir2/file1.jpg dir2.jpg
mv dir3/file1.jpg dir3.jpg

回答by mirandes

You can go inside each dir and run:

您可以进入每个目录并运行:

$ mv `ls | head -n 1` ..

回答by Olaf Dietsche

Look for all first level directories, identify first file in this directory and then move it one level up

查找所有第一级目录,识别此目录中的第一个文件,然后将其向上移动一级

find . -type d \! -name . -prune | while read d; do
    f=$(ls $d | head -1)
    mv $d/$f .
done

回答by electrovir

Building on the top answer, here is a general use bash function that simply returns the first path that resolves to a file within the given directory:

建立在最佳答案的基础上,这是一个通用的 bash 函数,它只返回解析为给定目录中的文件的第一个路径:

getFirstFile() {
    for dir in ""; do
        for file in "$dir"*; do
            if [ -f "$file" ]; then
                echo "$file"
                break 1
            fi
        done
    done
}

Usage:

用法:

# don't forget the trailing slash
getFirstFile ~/documents/

NOTE:it will silently return nothing if you pass it an invalid path.

注意:如果您向它传递无效路径,它将无声地返回任何内容。