我指的是命令的例子是ls,pwd和cd。另外,这些是如何构建的?你有例子吗?
我正在尝试在类 Unix 系统中模拟路径解析过程(请参阅手册页 path_resolution)。
我的操作系统是带有 GNU coreutils 8.7 的 Linux。
为了澄清分辨率中额外尾随 '/' 的含义,我在 shell 中做了以下事情:
mkdir this_is_dir
ln -s this_is_dir this_is_link
rm this_is_link
Run Code Online (Sandbox Code Playgroud)
一切都很好,因为 this_is_link 是一个符号链接,我只是将它删除了。但是在尝试时:
mkdir this_is_dir
ln -s this_is_dir this_is_link
rm this_is_link/
Run Code Online (Sandbox Code Playgroud)
它回响了 rm: cannot remove 'this_is_link/': Is a directory
好吧,我想,尾随的“/”导致了符号链接的跟随。所以,我尝试了另一个命令:rmdir this_is_link/
一个有趣的结果出来了: rmdir: failed to remove 'this_is_link/': Not a directory
不是我所期望的。所以我请我的朋友确认是否可以在他的系统上获得相同的结果。他的 coreutils 版本比我低。而结果是惊人的,无论rm或者rmdir 'this_is_link/',相同的错误Not a directory发生。
另一个朋友刚刚在他的Mac OS上试了一下,结果是:rm=> '是一个目录',rmdir=> 目录删除成功,链接仍然存在。
是否有任何关于路径解析的确切行为的规范?
我有一个包含大约 100,000 个文件的目录。ls -f 挂了超过一分钟。我运行了 strace,并立即开始看到 getdents,所以 ls 显然正在读取目录。我还看到很多对 brk 的调用,所以 ls 显然是在内存中缓冲内容。我编写了一个调用 readdir 并输出文件名的简单程序,它立即响应。但是 ls -f 不提供输出。是什么赋予了?我认为 -f 的全部意义在于它导致 ls 除了 readdir 什么都不做。是否有一种可移植、可靠的方法来列出目录的内容?(注意,这是来自 Linux 上 gnu 的 coreutils 的 ls。)
-编辑-
有一个别名,但是“/bin/ls -1f > /dev/null”需要 8 到 15 秒,“/bin/ls -1fx > /dev/null”需要 4 到 11 秒,但是一个简单的程序这只是 readdir 需要 0.011 秒。我需要做什么才能使 gnu ls 不烂?
我\xc2\xa0想要一个\xc2\xa0脚本来自动将所有文件\n移动到我\xc2\xa0已经到位的\xc2\xa0目录结构\n(看起来像~/jpg、~/png、~/mp3、~/zip等等)。\xc2 \xa0\n到目前为止,这几乎完全符合我的要求:
#!/bin/zsh\necho "Executing Script"\nfind . -iname \'*.jpg\' -exec gmv -i --target-directory=$HOME/jpg \'{}\' +\nfind . -iname \'*.png\' -exec gmv -i --target-directory=$HOME/png \'{}\' +\nRun Code Online (Sandbox Code Playgroud)\n我没有 shell 脚本经验,所以这只是我拼凑出来的东西。
\n只是在连续行上发出连续命令\n\n\n\n\n\n\n是为 shell 编写脚本的正确方法吗? \xc2\xa0\nI\xc2\xa0 最初尝试了此操作,\xc2 mv\xa0 涉及到错误处理\n\n\xc2\xa0it 只是不优雅。\xc2\xa0\n我\'m\xc2\xa0试图在这里扩展用户EvilSoup的答案。
除了我已经提到的之外,我还包含了该mv -i标志,这样我就不会覆盖任何已经存在的内容(这部分非常重要),但也许这-n更好,我不太确定。
关于find,我\xc2\xa0希望mv操作仅发生在当前目录中,并且由于某种原因,\nfind似乎有点递归,\虽然我不完全明白如何限制它。\xc2\xa0\ nI\xc2\xa0 想要在每个目录中运行我的脚本,并只运行在当前工作目录中找到的mv文件。find
+1 对于任何 zsh-on-macOS特定的介绍性材料:shell 脚本。
\n$ \time --version
time (GNU Time) UNKNOWN
Copyright (C) 2018 Free Software Foundation, Inc.
...
$ /bin/true --version
true (GNU coreutils) 8.32
Copyright (C) 2020 Free Software Foundation, Inc.
...
$ \true --version
$ echo 'nothing was printed on the previous line'
Run Code Online (Sandbox Code Playgroud)
我使用的是 Ubuntu 22.04,GNU bash,版本 5.1.16(也使用 zsh 5.9 进行了测试)。
Unix 实用程序,如 sort、find、grep、diff 等,可以非常方便地执行快速任务,有时根本不需要编写任何代码。
我想知道他们内部使用什么算法,以及如何智能地为特定任务决定特定算法?例如,如果 sort 得到一个巨大的输入文件,它会针对不同的数据大小使用不同的算法吗?
grep 在搜索不同数据集时是否智能切换算法?
我知道,cp有一个--reflink选项来控制完整副本与副本上写的“副本”。
在 btrfs 上,我可以使用ls(或其他一些命令)来确定文件是否与另一个文件共享(在写时复制的意义上)某些存储?
编辑:@StéphaneChazelas 将我指向filefrag,但这对我来说失败了:
root@void:/tmp/mount# mount | tail -1
/tmp/back on /tmp/mount type btrfs (rw,relatime,space_cache)
root@void:/tmp/mount# df -h | tail -1
/dev/loop0 32M 13M 20M 38% /tmp/mount
root@void:/tmp/mount# ls -lh
total 8.0M
-rw-r--r-- 1 root root 8.0M Jan 19 08:43 one
root@void:/tmp/mount# cp --reflink=always one two
root@void:/tmp/mount# sync
root@void:/tmp/mount# ls -lh
total 16M
-rw-r--r-- 1 root root 8.0M Jan 19 08:43 one
-rw-r--r-- 1 root root 8.0M Jan 19 08:45 …Run Code Online (Sandbox Code Playgroud) 我试图比较写入 GPFS 文件系统中的文件时的总写入速率,与直接写入具有 Red Hat Enterprise Linux Server 6.4 (Santiago) 的系统上的磁盘相比。对于我的应用程序,我需要测量原始速率,即不利用缓存。我不明白用于dd绕过缓存的直接选项的影响。oflag=direct与写入 GPFS 文件系统中的文件相比,直接写入块设备时,使用 时我获得的速率要低得多。为什么会发生这种情况?
为了测量聚合速率,我创建了并发写入块设备或文件的p进程dd。然后我将p获得的速率相加以获得总写入速率。
#!/bin/bash
directdiskrate=~/scratch/rate5
syncdiskrate=~/scratch/rate4
filerate=~/scratch/rate3
numruns=1
numthreads=30
#to disk use both conv=fsync and oflag=direct
writetodiskdirect="dd if=/dev/zero of=/dev/sdac bs=256k count=4096 conv=fsync oflag=direct iflag=fullblock"
for p in $(seq $numthreads)
do
#parses output of dd, rate is on last line, each field separated by ,s
$writetodiskdirect 2>&1|tail -n 1|awk 'BEGIN { FS = "," } ; { print $3 …Run Code Online (Sandbox Code Playgroud) 在echo中coreutils似乎是无处不在的,但不是每个系统都会有它在同一个地方(一般/bin/echo)。在echo不知道它在哪里的情况下调用它的最安全方法是什么?
如果echo系统上不存在coreutils二进制文件,我对命令失败感到满意——这比回显与我想要的不同。
注意:这里的动机是找到echo二进制文件,而不是找到一组参数,其中每个 shell 的echo 内置函数都是一致的。例如,似乎没有一种方法可以通过内置的 echo 安全地打印连字符,而不知道您是否在zsh或 中bash。
该dd 手册是在这个非常有限的:`
status=LEVEL
The LEVEL of information to print to stderr; 'none' suppresses
everything but error messages, 'noxfer' suppresses the final
transfer statistics, 'progress' shows periodic transfer
statistics
Run Code Online (Sandbox Code Playgroud)
这并没有说明实际输出,当我复制一个大图像文件时,它看起来像这样,例如:
dd if=input.img of=output.img status=progress
结果是:
到目前为止的传输速度和复制时间是显而易见的,但前三个数字是什么意思?
coreutils ×10
linux ×4
bash ×1
btrfs ×1
command-line ×1
dd ×1
directory ×1
echo ×1
filenames ×1
files ×1
filesystems ×1
gnu ×1
grep ×1
hard-disk ×1
ls ×1
macos ×1
performance ×1
portability ×1
shell ×1
shell-script ×1
sort ×1
source ×1
symlink ×1
ubuntu ×1
zsh ×1