Cannot stat no such file or directory ошибка

Using mv to move files and directories has become an everyday task for every Linux user, regardless of the distro they use. Sometimes, users can come across several common error messages written in weird languages, which causes confusion for beginners. In this article, we want to tackle one of the most popular error when using mv or cp in Linux : the “cannot stat” error.

Also see: How to fix “errno 5 input/output error”

What does “cannot stat” means

Stat is a program that obtains information about a file or directory in Linux. Because in Linux, each program should do one thing only and rely on one another to do other things, programs that perform file system operation should use stat as a standard way to retrieve details like metadata, size, modified date, permissions, etc.

Like any other program, before performing any operation, mv or cp must know some information about the source file/directory and the destination one. The default way to do that is by using stat.

Calling stat on a directory only requires execute permission of the directory containing the file. No read permission of the files is needed.

With that in mind, _every time you see “cannot stat” when using mv, you can safely assume that stat failed to obtain information about the file. In this case, the problem should be in the file/directory itself or the storage device.

Fixing cannot stat ‘filename’: No such file or directory

The error usually means the destination file or directory cannot be found by the system, so it cannot retrieve information.

If you come across “cannot stat” with “No such file or directory” message, checks the destination path first and then the source path for their correctness.

To get the correct full path of any directory in Ubuntu, you can cd to it, then run pwd to obtain the path. Remember to quote this path in any command to avoid errors caused by whitespaces and special characters.

Fixing cannot stat: permission denied

This error means that stat cannot read information about the file because it was denied by disallowing “execute” permission.

You should check the permission of the directory containing the file and the file itself. A quick and dirty solution is using chmod to recursively change the directory permission and all of its contents to 755.

You may need to elevate to root privileges using sudo if you’re not the owner of the folder. However, any changes to protected directories could cause system instability, and we advise carefully examine the directory before recursively chmod it.

Fixing cannot stat ‘foldername’: Input/output error

Among all “cannot stat” errors, the one with “Input/output error” messages is the hardest to debug and fix. In most of the cases, getting this error means there is some kind of file system corruption.

Input/output error in NTFS partitions

Usually, when beginners encounter “Input/output error”, they are accessing files from NTFS-formatted partitions. 

NTFS is Microsoft’s file system standard and has been famous for its complex permission system. Linux is able to manage these partitions thanks to a system program named ntfs-3g, but sometimes it cannot read the disk because of a minor corruption in data.

In this case, you should consider using running a program bundled in ntfs-3g called ntfs-fix. But before performing any fixes to the file system, please be aware that it could result in serious data loss.

If you need an open-source backup solution that could work with cloud storage services, be sure to check out rclone. You can get an easy head start without learning too much about the tool with our guide on rclone common commands.

If you see a “device is corrupted” message, back up your files immediately while you still can, then format the whole drive and check for errors before storing any important data on it.

Input/output error in ext partitions

First, ensure the filesystem is unmounted. If the file system is your root partition then you need to boot into rescue mode or use a live CD/USB flash drive so you can check the filesystem while it is unmounted. 

To run a filesystem check, use the command e2fsck. For example, if you are getting the error on a filesystem that is mounted on the partition /dev/hda4 run the following command:

e2fsck /dev/hda4

If the above command returns with errors about a “bad superblock” then run the following command:

dumpe2fs /dev/hda4

Locate the block number of Backup superblock in the output:

Group 1: (Blocks 8193-16384)

**Backup superblock** at **8193**, Group descriptors at 8194-8194

Block bitmap at 8195 (+2), Inode bitmap at 8196 (+3)

Inode table at 8197-8447 (+4)

7937 free blocks, 2008 free inodes, 0 directories

Run e2fsck again, this time with the backup superblock number:

e2fsck -b **8193** /dev/hda4Code language: JavaScript (javascript)

If this does not solve your problem, then reboot the system. Upon booting, a filesystem check will probably be forced. If you boot to a shell, run e2fsck on the partition that is reporting the errors.

I’m trying to create tar.gz file using the following command:

sudo tar -vcfz dvr_rdk_v1.tar.gz dvr_rdk/

It then start to create files (many files in folder), but then I get the following error:

tar: dvr_rdk_v1.tar.gz: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

I don’t see any description of this error, what does it mean?

Kusalananda's user avatar

Kusalananda

316k36 gold badges623 silver badges923 bronze badges

asked Aug 10, 2014 at 7:34

ransh's user avatar

Remove - from vcfz options. tar does not need hyphen for options.

With a hyphen, the argument for the -f option is z. So the command is in effect trying to archive dvr_rdk_v1.tar.gz and dvr_rdk into an archive called z. Without the hyphen, the semantics of the options changes, so that the next argument on the command line, i.e. your archive’s filename, becomes the argument to the f flag.

Also check your write permission to the directory from which you are executing the command.

Kusalananda's user avatar

Kusalananda

316k36 gold badges623 silver badges923 bronze badges

answered Aug 10, 2014 at 7:42

Velnix's user avatar

VelnixVelnix

1,1128 silver badges3 bronze badges

6

The -f option should directly precede the filename. So, use tar -vczf filename.tar.gz instead of -vcfz

Bernhard's user avatar

Bernhard

11.9k4 gold badges58 silver badges69 bronze badges

answered Aug 10, 2014 at 7:56

twan163's user avatar

twan163twan163

5,5704 gold badges13 silver badges17 bronze badges

2

The tar command historically has been one of the few commands that doesn’t follow the Unix utility syntax guidelines.

The standards page for tar says:

f
Use the first file operand (or the second, if b has already been specified) as the name of the archive instead of the system-dependent default

While the syntax guidelines include this:

Guideline 5:
One or more options without option-arguments, followed by at most one option that takes an option-argument, should be accepted when grouped behind one ‘-‘ delimiter.

So while the command you typed, tar -vcfz dvr_rdk_v1.tar.gz dvr_rdk/, would be fine on older versions of tar, certain versions of tar that are written to strictly follow the utility syntax guidelines will parse this to mean «use z as the file argument to -f«. So you should use the following to be portable:

tar -cvzf dvr_rdk_v1.tar.gz dvr_rdk/

answered Aug 10, 2014 at 8:33

Mark Plotnick's user avatar

Mark PlotnickMark Plotnick

24.8k2 gold badges58 silver badges80 bronze badges

Notice that the -f needs to go immediately before the archive name
but after the --exclude options in this Makefile multi-line example…

-f $(HOME)/.bitcoin-$(TIME).tar.gz $(HOME)/.bitcoin'  

#######################
#Backup $HOME/.bitcoin — Makefile usage example
########################

TIME=$(shell date +%s)  
export TIME  
init:  
    @echo ''  
    bash -c 'mkdir -p $(HOME)/.bitcoin'  
    bash -c 'conf/get_size.sh'  
    bash -c 'tar czv --exclude=*.log --exclude=banlist.dat   
            --exclude=fee_exstimates.dat --exclude=mempool.dat   
            --exclude=peers.dat --exclude=.cookie   
            --exclude=.lock --exclude=.walletlock   
            -f $(HOME)/.bitcoin-$(TIME).tar.gz $(HOME)/.bitcoin'  
    #install current bitcoin.conf  
    bash -c 'install -v conf/bitcoin.conf $(HOME)/.bitcoin'  
    @echo ''  

Hauke Laging's user avatar

Hauke Laging

86.6k18 gold badges124 silver badges173 bronze badges

answered Jul 18, 2020 at 21:16

RandyMcMillan's user avatar

For future readers, this post was the answer for me. Keep wildcards outside the quotes: tar -zcf archive.tar.gz "file-0"*

answered Nov 2, 2022 at 23:58

bfuzze's user avatar

You must log in to answer this question.

Not the answer you’re looking for? Browse other questions tagged

.

I have this directory called «mock», which contains 3 directories. I am trying to copy all the items from «mock» directory into the «projweek» directory using the following command:

cp  /mock/* ~/projweek

But I get this error:

cp: cannot stat ‘mock/*’: No such file or directory

Any ideas as to why that is?

isapir's user avatar

isapir

20.7k13 gold badges114 silver badges115 bronze badges

asked Dec 13, 2015 at 17:39

user5647516's user avatar

2

If your source directory is set in quotes, then make sure that the * is outside the quotes, i.e.

cp "source/"* dest

or

cp "source"/* dest

answered Jul 9, 2018 at 6:45

isapir's user avatar

isapirisapir

20.7k13 gold badges114 silver badges115 bronze badges

5

It’s an odd thing about the unix system that glob expansion (aka use of the «*«) is done by the shell, and not by the program you are calling, and furthermore, if the glob doesn’t match anything, instead of expanding to nothing, it expands to itself and passes that to the program. So the cp command sees literally «/mock/*» which doesn’t exist, because you have no file called «*«. Somewhat perversely if you had a file called «*» it would dutifully copy it without complaining.

answered Aug 5, 2020 at 12:46

xpusostomos's user avatar

xpusostomosxpusostomos

1,23810 silver badges14 bronze badges

2

cannot stat = file/dir does not exist. Check the path first.

And, you say you want to copy /mock but the error message says mock. Show the real code first.

When I test in ubuntu, cp (GNU coreutils) 8.28, I have no problem with copying all files under a dir to another dir, when both paths are correct.

root@DESKTOP-9NHNV2I:~# cp /root/temp/* /root
root@DESKTOP-9NHNV2I:~# ls
temp  test.txt  test2.txt  test3333.txt

answered Aug 5, 2019 at 12:18

WesternGun's user avatar

WesternGunWesternGun

10.9k3 gold badges87 silver badges151 bronze badges

cp is used in unix/linux for copy

cp /mock/* ~/projweek this means copy from /mock folder all files to folder projweek that resides in root

This means cp: cannot stat ‘mock/*’: No such file or directory unable to copy all files from mock folder because file or directory not exists on relevant path

answered Dec 13, 2015 at 17:46

2

cp: cannot stat ‘mock/*’: No such file or director
  1. Check that the files exist on the path.
  2. Also to copy all the files in a folder to another location, use . operator like: cp /source/. /dest/

answered Jul 12, 2021 at 19:30

Caffeine Coder's user avatar

0

When I configured shell script on jenkins(See the following lines), I got this error «cp cannot stat … No such file or directory».

ssh user@remoteNode
cd /somedir
cp fromdir/xxfile todir/xxfile

The following command solves my problem.

ssh user@remoteNode "cd /somedir; cp fromdir/xxfile todir/xxfile"

Why?
Double quotation marks are required. If not, the cp command will be executed locally.
Thanks to CDSN blogger Jinking01.

answered Dec 24, 2021 at 3:32

Tansy Z's user avatar

Tansy ZTansy Z

1692 silver badges2 bronze badges

1

I’m trying a simple bash script to backup my source files from my website root directory to my /home/backups/files directory.

script2.sh

#!/bin/bash
#START
TIME=`date +%b-%d-%y`
FILENAME=backup-$TIME.tar.gz
SRCDIR="var/www/html/My_Site"
DESDIR="/home/backups/files"
tar cpzfP $DESDIR/$FILENAME $SRCDIR
#END

Error:

tar: /var/www/html/My_Siter: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
  1. I cross checked for Whitespaces in each line of code, there are none.

  2. Cross checked the folder permission for both SRC and DES, its 0777.

I’m not getting any idea as to why I get
Cannot stat: No such file or directory.

asked Nov 28, 2016 at 7:43

Sushivam's user avatar

SushivamSushivam

1532 silver badges11 bronze badges

3

It looks to me like you script has Windows carriage returns in it, which are causing the failure. The clue to this is the r at the end of the path in the SRCDIR variable, which is shown in the error code as /var/www/html/My_Siter. Obviously that path does no exist with the added r on the end.

Here’s a sed command which you can run on your script to remove the carriage returns, it will make a backup of the original script, named script2.sh.bak.

sed -i.bak 's/r//g' script2.sh

The script should work correctly now, you can delete the backup version once you’ve verified this.

Another method to get rid of those unwanted carriage returns would be to use tr -d 'r' script2.sh, but you’d need to direct the output to a new file, and copy that in place of the old one.

One other thing that would be useful would be to ensure that you quote your variables when you use them in your script. It’s a good practice to get into.

answered Nov 28, 2016 at 10:48

Arronical's user avatar

ArronicalArronical

19.6k17 gold badges71 silver badges128 bronze badges

1

Do you have rights to write into DESDIR folder?

Also, to avoid «tar: Removing leading `/’ from member names» try this:

tar cpzfP $DESDIR/$FILENAME $SRCDIR

I don’t know why, but this works for me.

muru's user avatar

muru

191k52 gold badges468 silver badges719 bronze badges

answered Nov 28, 2016 at 8:38

Maksim Garkavenkov's user avatar

1

If you work with files and directories on a Unix-based system, you might have encountered the «cp cannot stat no such file or directory» error message while trying to copy files from one location to another. This error message indicates that the file or directory you are trying to copy does not exist or cannot be accessed by the system. This error can be frustrating, especially when you are handling important files.

In this guide, we will discuss what causes the «cp cannot stat no such file or directory» error and how to troubleshoot the issue. We will also provide you with step-by-step solutions to help you resolve file transfer issues on your Unix-based system.

What Causes the «cp cannot stat no such file or directory» Error?

The «cp cannot stat no such file or directory» error can be caused by a variety of factors, including:

  • The file or directory you are trying to copy does not exist or has been deleted.
  • The file or directory you are trying to copy is located in a directory that you do not have permission to access.
  • The file or directory you are trying to copy has a name that contains special characters or spaces.
  • The source or destination path is incorrect or misspelled.

How to Troubleshoot the «cp cannot stat no such file or directory» Error

To troubleshoot the «cp cannot stat no such file or directory» error, follow these steps:

Check if the file or directory exists: Verify that the file or directory you are trying to copy exists in the specified location. You can use the ls command to list the contents of the directory and check if the file or directory is present.

Check file or directory permissions: Ensure that you have the necessary permissions to access the file or directory. You can use the ls -l command to check the file or directory permissions.

Check file or directory name: If the file or directory name contains special characters or spaces, enclose the file or directory name in quotes. For example, if the file name is «my file.txt», use the command cp "my file.txt" /destination instead of cp my file.txt /destination.

  1. Check source or destination path: Verify that the source or destination path is correct and spelled correctly. If you are copying files from another directory, specify the absolute path instead of the relative path.

FAQ

Q1. What is the «cp» command in Unix?

The «cp» command in Unix is used to copy files or directories from one location to another.

Q2. How do I copy a file in Unix?

To copy a file in Unix, use the cp command followed by the source file and destination directory. For example, to copy a file named «file.txt» to a directory named «docs», use the command cp file.txt docs/.

Q3. How do I copy a directory in Unix?

To copy a directory in Unix, use the cp command with the -r option followed by the source directory and destination directory. For example, to copy a directory named «mydir» to a directory named «backup», use the command cp -r mydir backup/.

Q4. Why do I get the «cp cannot stat no such file or directory» error?

You might get the «cp cannot stat no such file or directory» error if the file or directory you are trying to copy does not exist or has been deleted, or if you do not have the necessary permissions to access the file or directory.

Q5. How do I fix the «cp cannot stat no such file or directory» error?

To fix the «cp cannot stat no such file or directory» error, verify that the file or directory exists, check file or directory permissions, check file or directory name, and check source or destination path.

Getting an error cp: cannot stat when trying to copy files from one folder to another

I have this directory called "mock", which contains 3 directories. I am trying to copy all the items from "mock" directory into the "projweek" directory using the foll…

Stack Overflowuser5647516

Понравилась статья? Поделить с друзьями:

Не пропустите эти материалы по теме:

  • Яндекс еда ошибка привязки карты
  • Cannot set allocations ошибка
  • Cannot run in this locale ошибка
  • Cannot resolve symbol string java ошибка
  • Cannot resolve symbol java intellij idea ошибка

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии