- mv .. are the same file
- lsattr $DEST_DIR
- Why do I get ‘X’ and ‘Y’ are the same file when using mv, even though they are clearly NOT the same file?
- 1 Answer 1
- Fastest way to tell if two files have the same contents in Unix/Linux?
- 8 Answers 8
- How to rename and move files and directories on Linux
- Overview
- Rename files on Linux
- Moving a file on Linux
- Moving and Renaming files on Linux
- Moving Multiple files on Linux
- Moving Directories on Linux
- Verbose Output Flag
- Do Not Overwrite Existing Files
- Do Not Prompt to Confirm Overwrites
- Same file, different filename due to encoding problem?
- 2 Answers 2
mv .. are the same file
В скрипте происходит перемещение файла.
Смонтирована SMB папка.
Выполняется команда: mv $src_dir/*.zip $dst_dir
При выполнении скрипта всё окей. Перемещение проходит корректно.
Но если вытащить вручную этот файл из папки spool и переложить обратно в folder1, то при выполнении скрипта, выдаёт: . are the same file.
На момент выполнения папка Spool пустая, то есть не идёт перезапись.
А при чём тут баш? А при чём тут mv?
включи set -x и посмотри внимательно, что на самом деле у тебя выполняется. Может * неправильно раскрывается, а может ты в путях напутал (в посте у тебя опечатка «filder1»).
Тут только проверять выполнение по шагам и искать либо свой косяк, либо аномалию/баг.
Нет, опечаток точно нет.
Выполняю вручную без скрипта
mv test.zip /mnt/folder1/spool
Через Windows переместил файл обратно.
Выполнил ls -a /mnt/folder1/spool — пусто
Выполняю команду mv test.zip /mnt/folder1/spool повторно, получаю are the same file
Если выполнить mv -f test.zip /mnt/folder1/spool, проблема не решается
Если выполнить mv -f test.zip /mnt/folder1/spool test.zip, то перемещение проходит корректно.
Но в моём случае файл не имеет статичного имени и требует использовать *zip
Это какая-то SMB-специфичная хрень. Какое-то кеширование, возможно. Добавь smb/samba в теги, может кто-нибудь кто разбирается в этом вендоговне подписан на тег.
Если выполнить mv -f test.zip /mnt/folder1/spool test.zip, то перемещение проходит корректно.
— эта команда не имеет смысла и не должна работать.
Нашёл парочку подобных случаев, вроде бы решалось перемонтирование smb с параметром cache=none.
mount -t cifs -o username=foo, password=bar, rw, cache=none ../../..
Что то без изменений.
Посмотрите, что происходит с файлами на самом самба сервере после каждой операции.
После проведения тестов:
Если выполнить на linux — mv $src_dir/*.zip $dst_dir
После выполнить перемещение обратно mv mv $dst_dir/*.zip $src_dir
А после снова mv $src_dir/*.zip $dst_dir
То ошибка не возникает.
То есть ошибка возникает, только если из папки назначения переместить файлы вручную из под windows.
Так же заметил, что ошибка возникает только именно при перемещении (вырезать-вставить).
То есть, если взять перемещённый файл из папки spool, скопировать его в folder1, а после в spool его удалить, то в дальнейшем команда mv выполняется без проблем.
Так же заметил, что ошибка возникает только именно при перемещении (вырезать-вставить).
то есть винда перемещает файл таким образом, что для клиента под линуксом этот файл остается там виден. не в буквальном смысле виден, а при попытке записи файла происходит такой возрат команде mv, который интерпретируется как будто файл там есть.
Говорю же, посмотрите на самом самба сервере, что там с файлами в этих каталогах в момент, когда на клиентах выдается ошибка.
lsattr $DEST_DIR
Думаю, что при перемещении файла назад в Windows OS не меняются аттрибуты «файла папки» в Linux-OS. И потом при попытке командой mv переместить файл — считиваются атрибуты и он там уже есть.
В данном случае могу посоветовать:
- переместить файл из $SRC_DIR в $DEST_DIR
- посмотреть lsattr $DEST_DIR
- из Windows-OS переместить файл из $DEST_DIR куда угодно
- посмотреть lsattr $DEST_DIR
Если в атрибутах осталась запись об «файле, которого нет» копать в ту сторону.
Как вариант, вместо mv попробовать использовать rsync
Источник
Why do I get ‘X’ and ‘Y’ are the same file when using mv, even though they are clearly NOT the same file?
I am attempting to move a file from one directory to another on a CIFS mounted file system using the command:
This produces the error message:
The target file in the . /daily/ directory definitely does not exist at this point.
This is the listing of the relevant directories after the error occurs:
The listing of the unmounted mount point shows nothing:
I followed the advice found here to disable CIFS caching, which works, but only the first time the mv is attempted. Thereafter it fails with the same error message.
What is going on here?
I am running under Ubuntu 18.04 and the mounted CIFS file system is on a remote Windows 10 machine. Also, The Windows machine is running a VirtualBox VM that also runs Ubuntu 18.04. The mv command works without issue when run from the VM.
1 Answer 1
They are the same file, in the sense that there would be a collision in these files during the move. It might be more clear if the language file name was used instead.
This is warning you that you’re moving files from the source to the destination, and that file, or filename already exists at the destination. This means that there is a chance that you will lose data. This existing file will be overwritten.
You can use —interactive or —force flags to change this behavior, but you must understand what you are doing.
When you issue the command the first time, some files are moved from the source to the destination, until this file is to be moved. At that time, mv errors and warns you about the situation. Be wary that at this point, you haven’t completed the move. You’re not copying, so many source files may not exist in the original directory.
If you know that you do not want the data currently at the destination /mnt/development/Backup/NagiosServer/daily/20190512-backup.zip , remove it before issuing the mv command, or forcefully overwrite existing files.
I recommend that you also look into rsync and its capabilities.
Источник
Fastest way to tell if two files have the same contents in Unix/Linux?
I have a shell script in which I need to check whether two files contain the same data or not. I do this a for a lot of files, and in my script the diff command seems to be the performance bottleneck.
Could there be a faster way to compare the files, maybe a custom algorithm instead of the default diff ?
8 Answers 8
I believe cmp will stop at the first byte difference:
I like @Alex Howansky have used ‘cmp —silent’ for this. But I need both positive and negative response so I use:
I can then run this in the terminal or with a ssh to check files against a constant file.
To quickly and safely compare any two files:
It’s readable, efficient, and works for any file names including «` $()
Because I suck and don’t have enough reputation points I can’t add this tidbit in as a comment.
But, if you are going to use the cmp command (and don’t need/want to be verbose) you can just grab the exit status. Per the cmp man page:
If a FILE is ‘-‘ or missing, read standard input. Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.
So, you could do something like:
EDIT: Thanks for the comments everyone! I updated the test syntax here. However, I would suggest you use Vasili’s answer if you are looking for something similar to this answer in readability, style, and syntax.
For files that are not different, any method will require having read both files entirely, even if the read was in the past.
There is no alternative. So creating hashes or checksums at some point in time requires reading the whole file. Big files take time.
File metadata retrieval is much faster than reading a large file.
So, is there any file metadata you can use to establish that the files are different? File size ? or even results of the file command which does just read a small portion of the file?
File size example code fragment:
If the files are the same size then you are stuck with full file reads.
Источник
How to rename and move files and directories on Linux
Overview
In this tutorial, you will learn how to use the mv command to move and renames files and directories on Linux.
Files and directories on Linux as very similar, from a filesystem point of view. This means the operations done on one can be also done on the other, with very few exceptions.
As such, you will notice that commands used to perform actions on files are identical with directories.
Rename files on Linux
To rename a file in Linux you use the mv command. The command accepts two or more arguments. For renaming files, only two arguments are needed, which are the source file and the target file.
The mv command will take the source file specified and rename it to the target file.
To rename a file named student1 to student10, for example, you would run the following command.
Provided the file target is the same directory, all file attributes will remain, including permissions.
Moving a file on Linux
To move a file to another location we use the same method as renaming a file, except the file path should be different.
For example, to move a file from /home/student1/lab-work.log to /var/labs/student1/lab-work.log , you would run the following command.
Moving and Renaming files on Linux
A file can be renamed during a move process using the mv command. You simply give the target path a different name. When mv moves the file, it will be given a new name.
For example, to move a file named student1.txt to /var/students and rename it to class1-student1.txt , you would run the following command.
Moving Multiple files on Linux
The mv command accepts multiple source files, which means we can move two or more files at the same time. When executing the mv command, each file listed will be considered a source with the last path being the exception. The last path will be treated as the target.
For example, to move student1.txt and student2.txt to /var/students , you would run the following command.
Moving Directories on Linux
Moving directories work the same as moving files. We specify the source directory and give a target directory.
For example, to move a directory path /tmp/logs to
/data/logs you would run the following command.
Moving Multiple Directories on Linux
As with files, multiple directories can be moved to a new location. We simply specially all of the directories to be moved, and then give a target directory for them to be moved to.
Verbose Output Flag
The mv command will perform its operations silently. No output will be printed to the screen while files or directories are being moved or renamed.
To instruct the mv command to print out a log of actions being taken, you can use the -v flag. This flag enabled verbosity, which is helpful for auditing.
Do Not Overwrite Existing Files
To force the mv command to not overwrite existing files when moving or renaming a file, use the -n flag.
In the example below, if the student2.txt file already exists, then the mv command will not rename the file and it will exit with an error.
Do Not Prompt to Confirm Overwrites
If you want to forcefully move files or directories and overwrite paths that already exist, you can use the -f flag. This is effective for overwriting old, stale files or directories with new ones with the same name.
Источник
Same file, different filename due to encoding problem?
I was about to diff a backup from it’s source to manually verify that the data is correct. Some chars, like åäö, is not shown correctly on the original data but as the clients (over samba) interpret it correctly it’s nothing to worry about. The data restored from backup shows the chars correctly, leading diff to not consider them to be the same files (with diffs, but rather completely different files).
md5 sums, same file but different name.
Mount options and filesystems
2 Answers 2
Unix filesystems tend to be locale-agnostic in the sense that file names consist of bytes and it’s the application’s business to decide what those bytes mean if they fall outside the ASCII range. The convention on unix today is to encode filenames and everything else in UTF-8, apart from some legacy environments (mostly Asian). Windows filesystems, on the other hand, tend to have an encoding that is specified in the filesystem properties.
If you need to work with filenames in a different encoding, create a translated view of that filesystem with convmvfs. See working with filenames in a different encoding over ssh
It appears that your original system has filenames encoded in latin-1. Your current system uses UTF-8, and the one-byte sequence representing å in latin-1 ( \345 ) is an invalid sequence in UTF-8 which ls prints as ? . Your backup process has somehow resulted in filenames encoded in UTF-8. Samba translates filenames based on its configuration.
To access the original files with your native encoding, make a recoded view:
(You may need other options depending on what permissions and ownership you want to obtain.)
Источник