Add file to directory linux

Recursively add a file to all sub-directories

How do I recursively add(or touch) a file into the current directory, as well as all sub-directories? For example,
I would like to turn this directory tree:

. ├── 1 │ ├── A │ └── B ├── 2 │ └── A └── 3 ├── A └── B └── I 9 directories, 0 files 
. ├── 1 │ ├── A │ │ └── file │ ├── B │ │ └── file │ └── file ├── 2 │ ├── A │ │ └── file │ └── file ├── 3 │ ├── A │ │ └── file │ ├── B │ │ ├── file │ │ └── I │ │ └── file │ └── file └── file 9 directories, 10 files 

5 Answers 5

find . -type d -exec cp file <> \; 
 -type c File is of type c: d directory -exec command ; Execute command; All following arguments to find are taken to be arguments to the command until an argument consisting of `;' is encountered. The string `<>' is replaced by the current file 

So, the command above will find all directories and run cp file DIR_NAME/ on each of them.

Also you may want to use a hard/soft link here since changing the file in every location can be a real pain

If you just want to create an empty file, you can use touch and a shell glob. In zsh:

shopt -s globstar for d in **/*/; do touch -- "$d/file"; done 

Portably, you can use find :

find . -type d -exec sh -c 'for d; do touch "$d/file"; done' _ <> + 

Some find implementations, but not all, let you write find . -type d -exec touch <>/file \;

If you want to copy some reference content, then you’ll have to call find in a loop. In zsh:

for d in **/*(/); do cp -p reference_file "$d/file"; done 
shopt -s globstar for d in **/*/; do cp -p reference_file "$d/file"; done 
find . -type d -exec sh -c 'for d; do cp -p reference_file "$d/file"; done' _ <> + 

When wanting to touch files called $name in the current directory and in all subdirectories, this will work:

Note that the comment by ChuckCottrill to the answer by terdon does NOT work, as it will only touch the file called $name in the current directory and the directory itself.

Читайте также:  Chown and chgrp in linux

It will not create files in subdirectories as requested by the OP, while this version here will.

Источник

Linux How to Add a File to a Directory: A Beginner’s Guide

Learn how to add a file to a directory in Linux with basic commands and techniques. Navigate to the directory, create a file, and add content to it with ease.

  • Navigating to the directory
  • Creating a file
  • Linux — How to Create Files and Directories
  • Creating a directory
  • Adding a file to a folder within a zip file
  • Other helpful code examples for how to add a file to a directory in Linux
  • Conclusion
  • How to add a file in Linux?
  • How do I add a file to a directory in terminal?
  • How do I add a file in Linux terminal?
  • How do I copy a file to a directory in Linux?

Linux is an open-source operating system that is widely used for servers and development environments. One common task when working with Linux is adding a file to a directory. This blog post will cover the basic commands and techniques for adding a file to a directory in Linux.

To add a file to a directory in Linux, the first step is to navigate to the directory using the cd command followed by the path to the directory. For example, if the directory is located in the user’s home directory, the command would be cd ~/directory . If the directory is located elsewhere, the full path can be used, such as cd /path/to/directory .

The cd command stands for “change directory” and is used to move between directories in Linux. The ~ symbol represents the user’s home directory. Using the full path to the directory ensures that the user is in the correct location before adding a file.

Creating a file

Once in the directory, there are multiple commands that can be used to create a file and add content to it. The touch command can be used to create an empty file, while the echo command can be used to add content to a file.

The touch command creates an empty file with the specified name. For example, to create a file named “example.txt”, the command would be touch example.txt .

The echo command is used to print text to the terminal or redirect it to a file. To add content to a file using echo , the output must be redirected to the file using the > symbol. For example, to add the text “Hello, World!” to a file named “example.txt”, the command would be echo «Hello, World!» > example.txt .

Читайте также:  Adobe audition linux аналоги

To create a file and add content to it in one command, the cat command can be used followed by the > symbol and the file name. For example, to create a file named “example.txt” with the text “Hello, World!”, the command would be cat > example.txt . Then type “Hello, World!” and press Ctrl+D to save the file.

Linux — How to Create Files and Directories

An introduction on how to create files and directories within the Linux operating system.**In Duration: 3:19

Creating a directory

If the directory does not exist, it must be created before adding a file. The mkdir command can be used to create a new directory.

The mkdir command stands for “make directory” and is used to create new directories or folders. The command can be followed by the name of the new directory or the full path to the new directory. For example, to create a directory named “new_directory” in the current directory, the command would be mkdir new_directory . To create a directory with a full path, the command would be mkdir /path/to/new_directory .

Adding a file to a folder within a zip file

To add a file to a specific folder within a zip file, the folder must first be created, and then the file can be moved into it.

The unzip command can be used to extract files and directories from a zip file. For example, to extract all files from “example.zip” into the current directory, the command would be unzip example.zip .

The mkdir command can be used to create a new folder within the extracted files. For example, to create a folder named “new_folder” within the extracted files, the command would be mkdir new_folder .

The mv command can be used to move the file into the new folder. For example, to move a file named “example.txt” into the “new_folder” folder, the command would be mv example.txt new_folder/ .

Other helpful code examples for how to add a file to a directory in Linux

In shell, linux how to add a file to directory code example

Conclusion

Adding a file to a directory in Linux is a simple task that involves navigating to the correct directory and using commands like touch or echo to create a file and add content to it. If the directory does not exist, use the mkdir command to create it before adding a file. Knowing these basic commands can improve productivity and efficiency when working with Linux.

In conclusion, adding files to directories in Linux is a fundamental task that every user must know. Properly navigating to the correct directory and creating files or directories using the cd , touch , echo , cat , and mkdir commands can significantly improve the efficiency and productivity of developers and system administrators. Additionally, being able to add files to folders within a zip file using the unzip , mkdir , and mv commands can be extremely useful in various situations. By following the techniques and commands mentioned in this guide, beginners and experienced users alike can easily add files to directories in Linux.

Читайте также:  Отвязать процесс от консоли linux

Источник

linux how to add a file to a specific folder within a zip file

If you need to add the file to the same folder as in the original directory hierarchy, then you just need to add the full path to it:

Otherwise, probably the easiest way to do that is to create the same layout you need in the zip file in a temporary directory.

if i wanna put yyy.txt into xxx.apk’s assests’ folder, firstly i must creat assests folder,then move yyy.txt into it. And then excute zip -g xxx.apk assests/yyy.txt

If you want to have the good hierarchy without copying files into a newly created dir, you can create a symbolic link : ln -s long_path_on_disk folder_in_zip. Then, zip -g xxx.zip folder_in_zip/file

To elaborate on @Ignacio Vazquez-Abrams answer from a year ago, you can use a lower level library, such as the one that comes with Python:

#!/bin/bash python -c ' import zipfile as zf, sys z=zf.ZipFile(sys.argv[1], "a") z.write(sys.argv[2], sys.argv[3]) z.close() ' myfile.zip source/dir/file.txt dir/in/zip/file.txt 

This will open myfile.zip and add source/dir/file.txt from the file system as dir/in/zip/file.txt in the zip file.

Info-ZIP cannot do this. You will need to write a script or program in a language that has lower-level access to zip files.

if i wanna put yyy.txt into xxx.apk’s assests’ folder, firstly i must creat assests folder,then move yyy.txt into it. And then excute zip -g xxx.apk assests/yyy.txt

Or you can generate an artificial zip entry that puts it in assets/ , then combine it with the data from the file and add it to the zip file.

I have expended a little @»that other guy» solution

Go to console, press ctrl+x,ctrl+e, paste there

( cat /tmp/zip-extend && chmod +x /tmp/zip-extend 

then run /tmp/zip-extend my_zip.zip your/existing/file_to_add.xml directory_in_zip/file_to_add.xml

cd /tmp touch first_file.txt zip my_zip.zip first_file.txt unzip -l my_zip.zip mkdir -p your/existing touch your/existing/file_to_add.xml /tmp/zip-extend my_zip.zip your/existing/file_to_add.xml directory_in_zip/file_to_add.xml unzip -l my_zip.zip cd - 
Archive: my_zip.zip Length Date Time Name --------- ---------- ----- ---- 0 2013-12-17 15:24 first_file.txt 0 2013-12-17 15:24 directory_in_zip/file_to_add.xml --------- ------- 0 2 files 

Источник

Оцените статью
Adblock
detector