Lab: Command-line solutions#
A#
To reproduce the file structure
└── MainFolder
├── NewFolder
│ ├── introduction.txt
│ └── LastFolder
│ └── data.txt
├── Readme.txt
└── SecondFolder
these are solutions without the editor at all, which contains redirection of output.
$ command > output.txt
means do not print the command output to the screen, instead save it in the file output.txt (overwrites if it already exists)
$ command >> output.txt
means do not print the command output to the screen, instead append it to the file output.txt (thus keeping old content)
if output.txt is a non-existing file the effect of both is the same
%%bash
mkdir MainFolder
cd MainFolder
mkdir NewFolder
cd NewFolder
pwd > introduction.txt
echo introduction.txt >> introduction.txt
mkdir LastFolder
cd LastFolder
pwd > data.txt
echo data.txt >> data.txt
cd ../..
pwd > Readme.txt
echo Readme.txt >> Readme.txt
mkdir SecondFolder
mkdir: cannot create directory ‘MainFolder’: File exists
mkdir: cannot create directory ‘NewFolder’: File exists
mkdir: cannot create directory ‘LastFolder’: File exists
mkdir: cannot create directory ‘SecondFolder’: File exists
---------------------------------------------------------------------------
CalledProcessError Traceback (most recent call last)
Cell In[1], line 1
----> 1 get_ipython().run_cell_magic('bash', '', 'mkdir MainFolder\ncd MainFolder\nmkdir NewFolder\ncd NewFolder\npwd > introduction.txt\necho introduction.txt >> introduction.txt\nmkdir LastFolder\ncd LastFolder\npwd > data.txt\necho data.txt >> data.txt\ncd ../..\npwd > Readme.txt\necho Readme.txt >> Readme.txt\nmkdir SecondFolder\n')
File ~/miniconda3/envs/bb1000/lib/python3.11/site-packages/IPython/core/interactiveshell.py:2430, in InteractiveShell.run_cell_magic(self, magic_name, line, cell)
2428 with self.builtin_trap:
2429 args = (magic_arg_s, cell)
-> 2430 result = fn(*args, **kwargs)
2432 # The code below prevents the output from being displayed
2433 # when using magics with decodator @output_can_be_silenced
2434 # when the last Python token in the expression is a ';'.
2435 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):
File ~/miniconda3/envs/bb1000/lib/python3.11/site-packages/IPython/core/magics/script.py:153, in ScriptMagics._make_script_magic.<locals>.named_script_magic(line, cell)
151 else:
152 line = script
--> 153 return self.shebang(line, cell)
File ~/miniconda3/envs/bb1000/lib/python3.11/site-packages/IPython/core/magics/script.py:305, in ScriptMagics.shebang(self, line, cell)
300 if args.raise_error and p.returncode != 0:
301 # If we get here and p.returncode is still None, we must have
302 # killed it but not yet seen its return code. We don't wait for it,
303 # in case it's stuck in uninterruptible sleep. -9 = SIGKILL
304 rc = p.returncode or -9
--> 305 raise CalledProcessError(rc, cell)
CalledProcessError: Command 'b'mkdir MainFolder\ncd MainFolder\nmkdir NewFolder\ncd NewFolder\npwd > introduction.txt\necho introduction.txt >> introduction.txt\nmkdir LastFolder\ncd LastFolder\npwd > data.txt\necho data.txt >> data.txt\ncd ../..\npwd > Readme.txt\necho Readme.txt >> Readme.txt\nmkdir SecondFolder\n'' returned non-zero exit status 1.
Windows:
mkdir MainFolder
cd MainFolder
mkdir NewFolder
cd NewFolder
cd > introduction.txt
echo introduction.txt >> introduction.txt
mkdir LastFolder
cd LastFolder
cd > data.txt
echo data.txt >> data.txt
cd ..\..
ls > Readme.txt
echo Readme.txt >> Readme.txt
mkdir SecondFolder
for this special case everything is the same except
cd
without arguments prints the current folderthe file path separator i the backslash
\
!tree MainFolder
MainFolder
├── NewFolder
│ ├── introduction.txt
│ └── LastFolder
│ └── data.txt
├── Readme.txt
└── SecondFolder
3 directories, 3 files
!cat MainFolder/NewFolder/introduction.txt MainFolder/NewFolder/LastFolder/data.txt MainFolder/Readme.txt
/home/python-lll/Documents/lecture-notes/MainFolder/NewFolder
introduction.txt
/home/python-lll/Documents/lecture-notes/MainFolder/NewFolder/LastFolder
data.txt
/home/python-lll/Documents/lecture-notes/MainFolder
Readme.txt
B#
How do we modify the existing structure such in order to obtain
└── MainFolder
├── FirstFolder
│ ├── data.txt
│ └── introduction.txt
└── SecondFolder
└── Readme.txt
rename NewFolder to FirstFolder
the data.txt should be moved to FirstFolder
LastFolder should be removed
move the Readme.txt to SecondFolsder
%%bash
cd MainFolder
mv NewFolder FirstFolder
cd FirstFolder/LastFolder
mv data.txt ..
cd ..
rmdir LastFolder
cd ..
mv Readme.txt SecondFolder
cd ..
Windows
cd MainFolder
ren NewFolder FirstFolder
cd FirstFolder\LastFolder
move data.txt ..
cd ..
rmdir LastFolder
cd ..
move Readme.txt SecondFolder
cd ..
Comment
Windows has different command for rename and move.
ren
orrename
to change a name of a filemove
to change the location of a file
!tree MainFolder
MainFolder
├── FirstFolder
│ ├── data.txt
│ └── introduction.txt
└── SecondFolder
└── Readme.txt
2 directories, 3 files
Finally edit the files to match their content to their new locations