【问题标题】:for: Command not found on gnome terminal?对于:在 gnome 终端上找不到命令?
【发布时间】:2023-02-24 16:24:26
【问题描述】:

简而言之:我有一个包含 1583 .tar 文件的文件夹。每个.tar 文件只包含一个文件夹,我想要一个只包含这些文件夹的文件夹。尝试使用控制台命令执行此操作,因为我不知道如何让 python 为我执行此操作。通过在线阅读,我想出了命令

for file in ls *.tar do; do tar -xzf $file -C /path/to/destination/folder done

出现错误:

for: command not found

不确定该怎么办。我到处都在寻找如何让 for 循环在 linux 终端上工作,到处都在说“

for x; do y

没有关于如何真正让这些东西工作的建议......

我听到很多关于 linux bash 和脚本之类的东西……我真的不明白这些东西是什么。我需要执行一些仪式才能让 bash 工作吗?显然 ls 和 cd 之类的东西也是 bash 的一部分?那些对我来说工作正常,但循环不是......

对不起,如果这真的是基本的家伙,我有点电脑文盲。

谢谢你的帮助!!

--

好的,它要求我写 2 个部分,在我已经写完上面的内容之后,我想出了以下内容:


如前所述,我想出了命令

for file in ls *.tar do; do tar -xzf $file -C /path/to/destination/folder done

手动使用 tar -xzf 工作得很好,甚至把它放在正确的文件夹中。但我不想手动执行此操作(因为有 1500 多个文件)并且 tar 命令不会将 *.tar 作为输入 - 我查找了如何解决此问题并获得了我尝试使用的命令作为修复。

再次感谢!!

裘德小号

【问题讨论】:

    标签: linux bash for-loop tar


    【解决方案1】:

    命令中存在一些语法错误。但是如果我在我的 bash-shell 中运行它,我不会收到你的 for: command not found 消息。那么,你确定你使用的是bash吗?

    可以肯定的是(出于测试目的)你可以输入

    bash
    

    这将为您启动一个 bash-shell。

    至于语法错误:命令可能应该是:

    for file in *.tar ; do tar xzf "$file" -C /path/to/destination/folder ; done
    

    【讨论】:

      【解决方案2】:
      If you are trying to use the for command in the GNOME Terminal and are receiving a "command not found" error, it may be because for is a shell keyword and not a standalone command.
      
      To use the for loop in the GNOME Terminal, you need to run it within a shell script or in an interactive shell session such as Bash. Here's an example of a simple for loop script:
      
      Open a text editor and create a new file, for example for_loop.sh.
      
      Add the following code to the file:
      
      #!/bin/bash
      for i in {1..10}
      do
          echo "Number: $i"
      done
      
      This code creates a for loop that iterates through the numbers 1 to 10 and prints each number to the terminal.
      
      Save the file and close the text editor.
      
      Open the GNOME Terminal and navigate to the directory where you saved the for_loop.sh script.
      
      Run the script by typing ./for_loop.sh and pressing Enter.
      
      You should now see the output of the for loop in the terminal.
      
      If you are trying to use the for command in a different context, please provide more information about what you are trying to do and the exact error message you are receiving, and I can try to provide more specific guidance.
      

      【讨论】:

      • 嗨,我试过这样做,但它说“权限被拒绝”
      • 请在上方查看我的第二个答案,希望对您有所帮助。
      【解决方案3】:

      如果您在终端中尝试执行命令时收到“权限被拒绝”错误,则很可能是您没有运行该命令所需的权限。以下是一些可以尝试的解决方案:

      使用 sudo:尝试使用 sudo 运行命令,这将为您提供更高的权限。为此,请键入“sudo”,然后键入要运行的命令,然后在出现提示时输入密码。

      检查权限:确保您具有执行命令所需的权限。您可以使用“ls -l”命令检查文件或目录的权限。如果该文件不可执行,您可以使用“chmod”命令更改其权限。

      检查所有权:确保您拥有要对其执行命令的文件或目录。您可以使用“ls -l”命令检查所有权。如果您不拥有该文件,则可能需要使用 sudo 或向所有者征求许可。

      检查文件类型:确保您尝试执行的文件实际上是可执行的。您可以使用“文件”命令检查文件类型。如果该文件不可执行,您可能需要修改它或通过可以解释其内容的程序运行它。

      如果这些解决方案均无效,请提供有关您尝试执行的命令的更多信息以及您收到的确切错误消息,我将尽我所能进一步帮助您。

      【讨论】:

      • 使用 root -privileges 运行具有语法错误的命令是灾难的根源。
      猜你喜欢
      • 2015-08-28
      • 2013-10-30
      • 2016-04-11
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      • 2021-07-12
      • 2011-07-16
      • 2016-10-01
      相关资源
      最近更新 更多